Narrow int casting to smaller types

If each operand in assignment and mathematical operations advances to before executing an expression int(if it has no flags L, f, d);

And adding intto a smaller primitive type (e.g. byte) should be done with narrow-casting;

So how does the following assignment work?

byte a = 100;

If 100 is an integer, therefore casting is required to enter it in bytes.

+4
source share
3 answers
byte a = 100;

This works because ...

If the right side in the context of the assignment is a constant expression,

, byte, short char, .

[...]

.

-

[...] -128 127 .

+2
byte a = 100; 

, java -128 to 127, , 127, .

128, .

byte a = 128 ; //compiler error(incompatible type)
byte a = (byte)128;
+2

, , . Java.

+1

All Articles