If I have the following:
byte[] byteArray = new byte[] {87, 79, 87, 46, 46, 46};
I know that the size of each element would be one byte. But what I don’t seem to understand is how would the integer 87 be stored in one byte? Or, how does the byte[] store data?
EDIT: I see that you can store -128 to 127 in a byte here in java. So, does that mean there is no way to store anything greater than or lesser than those numbers in a byte[]? If so, doesn’t that limit the use of this? Or am not understanding the exact places to use a byte[].
A byte is 8 bits.
2^8is 256, meaning that 8 bits can store 256 distinct values. In Java, those values are the numbers in the range -128 to 127, so 87 is a valid byte, as it is in that range.Similarly, try doing something like
byte x = 200, and you will see that you get an error, as 200 is not a valid byte.