What does this mean “Narrowing a primitive truncates the high order bits”

僤鯓⒐⒋嵵緔 提交于 2019-12-24 03:28:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!