java - Type Conversion and compile time constant -
byte b=5; integer i=(int)b;//b cast int , int wrapped integer integer k=(byte)b;//compilation error, cannot convert byte integer integer z=(byte)5;//compiles
my question why integer z=(byte)5
compiles while integer k=(byte)b
not? matter integer z1 = (byte)5l
, integer z2 = (byte)5.3f
compile. because attempting cast compile time constant , cast has no effect on it?
as assignment
integer z=(byte)5
uses literal value, translated compiler to
integer z = integer.valueof(5);
the compiler not smart enough reference variables in case:
integer k= (byte)b;
Comments
Post a Comment