Assign int to byte in Java?

int val = 233;
int val = 233;
byte b = (byte) val;
System.out.println(b);

I have a simple example: I have an integer with some value & I want to convert that value to bytes of output But in this case, negative values are coming

How to successfully put int value into byte type?

Solution

In Java byte, the range is - 128 to 127 You cannot store integer 233 in one byte without overflow

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>