byte b = 0xFFFFFFFF; //OK, because integer -1 sits between -128 and 127, FINE!!
char ch = 0xFFFFFFFF; //Not OK, because integer -1 does not sit between 0 and 65535, FINE!!
byte b = 0L; //Compiler says Not OK? But long integer 0 sits between -128 and 127?
I am not convinced with narrowing rule applied by the java compiler in third line of above code.
Please help me understand, the logic behind this narrowing rule.
The
Lsuffix on the literal0Lmakes this literal of typelong(a 64-bit signed integer).There is no implicit narrowing from
longtobyte, according to the rules of the Java language.See Java Language Specification section 5.2 Assignment Contexts:
Note that the type of the constant expression does not include
long.