Why when casting int c = 10000
to the type byte
, the value of the variable becomes 16
, not 128
, the maximum value of byte
?
int c = 10000; byte d = (byte) c; System.out.println(d);
Why when casting int c = 10000
to the type byte
, the value of the variable becomes 16
, not 128
, the maximum value of byte
?
int c = 10000; byte d = (byte) c; System.out.println(d);
You do not quite understand how type conversion occurs.
When casting int
to byte
, the “best fit” calculation does not occur. It is different: the “high bytes” are simply discarded.
10000 = 0x2710 consists of two bytes: 0x27 and 0x10. The most significant byte is discarded, the least significant 0x10 = 16 remains.
Because in byte
you can cram a maximum of 255. You have an overflow of the discharge grid. The size of the type byte
is 256
. Find the number that comes after the cast is simple - just take the module:
10000 % 256 = 16;
Source: https://ru.stackoverflow.com/questions/476378/
All Articles