本帖最后由 sd3560531 于 2011-06-28 16:27:47 编辑

解决方案 »

  1.   


    byte b= -1;//没有任何问题,因为byte可以表示-128到127之间的数
    System.out.println(0xFF);//255,这里的0xff实际上等价于0x000000ff,已经超过了127,当成了int
    byte s = -128;//没有任何问题,因为byte可以表示-128到127之间的数
    System.out.println(0x80);//128,同样的道理,等价于:0x00000080,已经超过了127,当成了int
    byte s2 = 0x7f;
    System.out.println(0x7f);//127,同样的道理,等价于:0x0000007f,没有超过127
      

  2.   

    我想问的是
    byte s2 = 0x80;为什么是错的?本来应该表示-128。
    如果说0x80 超过了127,那就应该自动转为负数啊。类似int就会自动转为负数。
    int i = 0x7fffffff;表示的int最大值
    而赋值int i = 0x80000000; 编译器没有认为超过,而至自动转负数了。表示int的最小值。
      

  3.   

    A hexadecimal numeral consists of the leading ASCII characters 0x or 0X followed by one or more ASCII hexadecimal digits and can represent a positive, zero, or negative integer. Hexadecimal digits with values 10 through 15 are represented by the ASCII letters a through f or A through F, respectively; each letter used as a hexadecimal digit may be uppercase or lowercase.
    0x开头的数字都是int类型,而不是byte或者short类型。
      

  4.   

    你的意思是:
    类似这样的赋值 byte b = 0x80;都存在内部类型转换,0x80表示128,内部转换的时候超过了127因此报错。
    而int i = 0x80000000;就不存在内部转换,直接把0x8000000应该表示的值赋值给i了。是这样吗?
      

  5.   


    是的,int是32位的楼主如此复制,没超过自然可行咯。