
这是因为a
short可以保持负值,而
char您可能从中看不到
Character.MIN_VALUE。让我举几个例子。
short s = -124; char c = 124; // OK, no compile time error char d = -124; // NOT OK, compile time error since char cannot hold -ve values char e = s; // NOT OK, compile time error since a short might have -ve values which char won't be able to hold char f = (char)s; // OK, type casting. The negative number -124 gets converted to 65412 so that char can hold it System.out.println((short)f); // -124, gets converted back to a number short can hold because short won't be able to hold 65412 System.out.println((int)f); // 65412, gets converted to 65412 because int can easily hold it.
A(负)号
-n转换为时
char,变得
2^16-n。因此,
-124成为
2^16-124 = 65412
我希望这有帮助。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)