why explicit type casting required from double to float but not from int to byte?

前端 未结 4 1388
失恋的感觉
失恋的感觉 2020-12-20 20:32

Consider following statement:

byte by = 5; //works fine

literal \'5\' is of type int and small enough to fit into a variable of type byt

4条回答
  •  醉话见心
    2020-12-20 21:22

    Perhaps the most significant reason that Java makes allowance for implicit narrowing conversions of literals of type int to short and byte, but does not do so for conversions of literal double values to float is that Java includes float literals, but does not allow literals of types byte and short.

    Personally, I really dislike Java's numerical conversion rules, but the allowance for storing integer constants to short and byte makes those types at least somewhat bearable.

提交回复
热议问题