It's not quite the same as rounding, though it is somewhat related because you just can't represent some values in a "smaller" type.
The fact is modern computers represent numbers in binary. A "smaller" type simply has less bits. According to the 5.1.3 of the Java Language Specifications
, down-casting for integral types (byte, char, short, int, long) takes the n least significant bits, where n is the number of bits in the target value.
// this is hexedecimal so I don't have to write 32-bit values in binary :P
int(0x12345678) = byte(0x78)
int(0x12345678) = short(0x5678)
In addition to this, Java uses two's compliment
to represent signed types.
So even though int(0x7FFF) = byte(0xFF), the integral value of the int is 32767, and the integral value of the byte is -1!
In Java you must make an explicit case from int to byte because of this. The reasoning is that this behavior is usually not expected (in what world does 32767 even come close to equal -1?), but it is very important to be able to make this case because the int value may not necessarily be out of range (int(1) = byte(1)), and there are many useful things you can do using bit-fields and bit-twiddling (mucking with the bits), though I have qualms with using Java with bit-fields and bit-twiddling.