Why in Java, in the process of data type coercion, the value exceeds the value range of the converted data type and the value is negative?
To fundamentally understand this process and principle, you need to understand two knowledge points:Java type conversionandbinary
1. Java type conversion
Java is a strongly typed language, and the object type needs to be declared before using variables; in the actual development process, data type conversion is often involved.
Data type conversion is divided into two types, one is implicit conversion :
public class testCode {
public static void main (String[]args){
char char_1 = 'a';
int number_2 = char_1 + 1;
System.out.println(number_2);
}
}
In int number_2 = number_1 + 1;
the process of , the initial declaration of char type char_1
will be implicitly converted to int type, and a
the corresponding ASSIC code of lowercase is 97 in decimal, so char_1 + 1
the result is 98.
Running results:
Although the automatic conversion is convenient, it strictly abides by the conversion rules (low to high):
byte > short > int > long >float > double
char > int > long >float > double
If you want to convert a value of type float to type int, you need to use coercion (high to low):
public class testCode {
public static void main (String[]args){
float float_1= 3.1415926F;
//强制转换格式:数据类型 变量名 = (转换类型) 被转换的变量;
int number_2 = (int) float_1;
System.out.println(number_2);
}
}
Although mandatory conversion is not difficult, the conversion process involves a big problem: loss of precision!
3. 1415926 is directly cut into integer 3. The reason for this situation is similar to the reason why the value exceeds the value range of the converted data type and the value is negative. Fundamentally, it is because the storage space of the basic data type is inconsistent.
Two, binary
Determined by the hardware of the computer, any data stored in the computer is essentially stored in binary code.
Taking one byte byte
and two bytes short
as an example, it is represented in computer memory as:
Binary storage of short and byte in memory:
1 Byte = 8bit, where the highest bit indicates the value is positive or negative.
Binary representation of positive and negative values:
As for why 10000000
it means -128 but 01111111
127, it involves the knowledge of the inverse code in the computer. For details, you can read the in-depth understanding and principle of the original code, inverse code, and complement code in this article . We only need to understand:
1. The highest bit is used to represent Positive or negative
2. 10000000
It means -128 and 01111111
means 127.
3. When the highest bit is negative, the value is equal to the sum of the lowest value of the value range and the remaining 7 bits; if the highest bit is positive, the sum of the remaining 7 bits is taken directly.
The calculation method of positive and negative binary values:
During the forced conversion process, due to the different size of the occupied space, the redundant space will be discarded directly. For example, if 1500ml of water is poured into a 1000ml bottle, only 1000ml can be saved in the end.
Binary representation of coercion:
If the highest bit of the last byte is 1 when coercing, it is negative:
The coerced binary negative representation:
The case where the value is negative is clarified, so what is the loss of precision?
In fact, the same is true:
Convert decimal to integer binary representation:
Run a program to verify
public class testCode {
public static void main (String[]args){
int a = 391;
byte b = (byte) a;
System.out.println(b);
}
}
result: