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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -