How are integers cast to bytes in Java?

后端 未结 8 1216
我在风中等你
我在风中等你 2020-11-27 21:02

I know Java doesn\'t allow unsigned types, so I was wondering how it casts an integer to a byte. Say I have an integer a with a value of 255 and I cast the integer to a byte

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 21:22

    The following fragment casts an int to a byte. If the integer’s value is larger than the range of a byte, it will be reduced modulo (the remainder of an integer division by the) byte’s range.

    int a;
    byte b;
    // …
    b = (byte) a;
    

提交回复
热议问题