Many children who have just studied Java do not understand why the int is sometimes turned into a negative number when strongly converted to byte.
This is because you do not understand the representation of numeric values in Java.
java specifies a negative number in the form of twos complement.
With the concept above, we know why the high-precision conversion of low-precision types can sometimes become negative.
For example, int 232 to byte value becomes-24
The conversion process is as follows:
232 is stored in the computer as follows:
00000000 00000000 00000000 11101000
To byte after the high-level storage is as follows:
11101000
The highest bit of this value is 1: So he is a negative number, expressed in twos complement. Then we can figure out what the value of this negative number is.
The inverse code is:
10010111
The original code is:
10011000
So his value is:-24
How values in Java are represented in a computer