I am about to start working on something the requires reading bytes and creating strings. The bytes being read represent UTF-16 strings. So just to test things out I wante
The key thing to remember here is that int
in Java is a signed value. When you assign 0xffffffff
(which is 2^32 -1
), this is translated into a signed int of value -1
- an int
cannot actually represent something as large as 0xffffffff
as a positive number.
So for values less than 0x80 and greater than 0xFFFFFF80, the resulting int
value is between -128 and 127, which can unambiguously be represented as a byte
. Anything outside that range cannot be, and needs forcing with an explicit cast, losing data in the process.