Converting long to byte in Java
•
Java
I can't understand the following:
In Java,
long l = 130L; byte b = (byte)l;
If I print the value of B, why do I get - 126? What does the long l stand for?
Solution
Bytes are signed in Java - so the range of values is - 128 to 127 inclusive
130 as long bit mode, when simply truncated to 8 bits, it is - 126 bit mode as byte
Another example:
int x = 255; byte b = (byte) x; // b is Now -1
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码