今天做leetcode题目时,发现int mid = 536848900,但是mid*mid=484528144,我说为啥程序运行一直出错呢!!
int mid = 536848900;
System.out.println(mid * mid);//484528144
long mid2 = 536848900;//long * long
System.out.println(mid2 * mid2);//288206741431210000
System.out.println(Integer.MAX_VALUE);//2147483647
System.out.println(Math.pow(2, 31) - 1);//2.147483647E9
int a = Integer.MAX_VALUE;
// a += 1;//-2147483648
a += 100;//-2147483549
long MonthNanoSeconds1 = 30 * 24 * 3600 * 1000 * 1000;//右边的结果是int,已溢出
//int * long,与最后一个long型的1000相乘之前就已经溢出,所以结果也不对
long MonthNanoSeconds2 = 30 * 24 * 3600 * 1000 * 1000L;
//这是正确的写法
long MonthNanoSeconds3 = 30L * 24 * 3600 * 1000 * 1000;//long * int
System.out.println(MonthNanoSeconds1);//2134720512
System.out.println(MonthNanoSeconds2);//-1702967296000
System.out.println(MonthNanoSeconds3);//2592000000000
System.out.println(30 * 24 * 3600 * 1000);//-1702967296
System.out.println(30 * 24 * 3600 * 1000 * 1000);//2134720512
在Java Language Specifictionz中所述(JSL 15.7.1)
If an integer multiplication overflows, then the result is the low-order bits of the mathematical product as represented in some sufficiently large two’s-complement format. As a result, if overflow occurs, then the sign of the result may not be the same as the sign of the mathematical product of the two operand values.
也就是说int型整数相乘,结果只会保留低32位,高位会抛弃掉。
加减乘除的运算符优先级都是从左向右。
参考:
《java int溢出总结》 https://njucz.github.io/2017/08/16/java-int%E6%BA%A2%E5%87%BA%E6%80%BB%E7%BB%93/
《java中short、int、long、float、double取值范围》 https://blog.csdn.net/qfikh/article/details/52832087
《[Java]运算符优先级参考图表》 https://blog.csdn.net/xiaoli_feng/article/details/4567184