When using Java for programming, when assigning an integer constant to a long variable, if the value exceeds the value range of the int data, the program will have a compile error, but there is a case where the program will not be faulted. is to assign multiple int data to a long variable, even if the result of the operation exceeds the range of int data, the program will not compile errors.
Test 1:
Long num=2147483648, when assigning a value to num, an error occurred, exceeding the range of values.
Test 2:
-
Package Test;
-
public class Test {
-
public static void Main (string[] args) {
-
Long num=2147483647*10;
-
SYSTEM.OUT.PRINTLN (num);
-
}
-
}
Operation Result:
650) this.width=650; "src=" Http://img.mp.itc.cn/upload/20160810/ae5ec17991a64c578d5a9ac39b019358.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>
Cause: The two integers participating in the calculation do not exceed the range of the int data, so there is no compilation error, and the result error is that when the two numbers are evaluated with the int data, the result is the int type, because the result of the calculation is beyond the range of the value of the int data. So the program overflows, but the program retains the result of the overflow, so the output is-10.
Test 3:
-
Package Test;
-
public class Test {
-
public static void Main (string[] args) {
-
Long num=2147483647*10;
-
SYSTEM.OUT.PRINTLN (num);
-
Long num1=2147483647l*10;
-
System.out.println (NUM1);
-
}
-
}
Operation Result:
650) this.width=650; "src=" Http://img.mp.itc.cn/upload/20160810/74f29eea37004be2aac310d574cc2f6e.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>
Cause: Because the uppercase letter L is added after the first integer constant in the calculated two integers, the operation to be performed is done by a long integer, so the program does not overflow and can calculate the correct result.
Technology sharing: www.kaige123.com
This article from the "11247808" blog, reproduced please contact the author!
Java Array Operations