问题
What does this mean "Narrowing a primitive truncates the high order bits"
回答1:
E.g. if you cast long to int you are discarding the higher bits of the long.
Short -> Byte
0x00FF -> 0xFF
256 -> -128
回答2:
Here's a short, carefully-chosen answer.
public class Narrow {
public static void main(String[] args) {
int i;
short s;
i = 32768;
s = (short) i;
System.out.println("int of " + i + " becomes a short of " + s);
}
}
i is 2^15, or one bigger than the MAX_VALUE of short.
Java will reply int of 32768 becomes a short of -32768
来源:https://stackoverflow.com/questions/6777905/what-does-this-mean-narrowing-a-primitive-truncates-the-high-order-bits