Java 8在Math类中引入了新方法,该方法将抛出ArithmeticException来处理溢出。 这些方法包括addExact , substractExact , multiplyExact , incrementExact , decrementExact和negateExact与int和long争论。 另外,还有一个静态的toIntExact方法将一个long值转换为一个也会引发ArithmeticException的int 。
在Java 8之前,程序员必须手动调试才能在溢出的代码中查找变量。 我很高兴发现Java 8引入了一组新的方法,这些方法在结果溢出时会引发异常。 对于许多开发人员而言,此更改不会产生重大影响,但是对于像我这样谦虚的开发人员而言,这肯定会影响花时间抓挠我们的头来理解为什么错误的结果,或者必须采取复杂的预防措施来避免溢出。
1. Math.multiplyExact
本示例说明了普通乘法与新multiplyExact之间的区别
MultiplyExact.java
package com.mkyong.exactmethods;
package com.mkyong;
public class MultiplyExact {
public static void main(String[] args) {
int x = Integer.MAX_VALUE; //( = 2 147 483 647)
int y = Integer.MAX_VALUE;
Object z;
System.out.println("---Before Java 8---");
z = x * y;
System.out.println("z : " + z);
System.out.println("\n---Since Java 8---");
try {
z = Math.multiplyExact(x, y);
} catch (ArithmeticException e) {
System.out.println(e.getMessage()); //Java 8 throws integer overflow
z = Math.multiplyExact((long) x, (long) y);
System.out.println("z : " + z);
}
if (z instanceof Long) {
System.out.println("\n> yuuuup z is Long");
}
}
}
输出:
---Before Java 8---
z : 1
---Since Java 8---
integer overflow
z : 4611686014132420609
> yuuuup z is Long
2.检测并处理溢出
我们处理integer溢出并确定long溢出的示例。
MultiplyExact2.java
package com.mkyong.exactmethods;
public class MultiplyExact2 {
public static void main(String[] args) {
int x = 1000000;
int y = 1000000;
long a = Long.MAX_VALUE; //( = 9 223 372 036 854 775 807)
long b = Long.MAX_VALUE;
Object z, c;
System.out.println("---Before Java 8---");
z = x * y;
c = a * b;
System.out.println("z : " + z);
System.out.println("c : " + c);
System.out.println("\n---Since Java 8---");
try {
z = Math.multiplyExact(x, y);
c = Math.multiplyExact(a, b);
} catch (ArithmeticException e) {
try {
z = Math.multiplyExact((long) x, (long) y);
c = null;
} catch (ArithmeticException ex) {
z = null;
}
}
if (z instanceof Integer) {
System.out.println("z is instance of Integer: " + z);
}
if (z instanceof Long) {
System.out.println("z is instance of Long: " + z);
} else {
System.out.println("Overflow for z");
}
if (c instanceof Integer) {
System.out.println("Instance of Integer: " + c);
}
if (c instanceof Long) {
System.out.println("Instance of Long: " + c);
} else {
System.out.println("Overflow for c");
}
}
}
输出:
---Before Java 8---
z : -727379968
c : 1
---Since Java 8---
z is instance of Long: 1000000000000
Overflow for c
3.所有xxxExact方法
演示所有新xxxExact方法的示例
AllExactMethods.java
package com.mkyong.exactmethods;
public class AllExactMethods {
public static void main(String[] args){
int x = 10000;
int y = 10000;
Object z;
z = Math.addExact(x, y);
System.out.println("addExact: " + x + " + " + y + " = " + z);
z = Math.subtractExact(x, y);
System.out.println("subtractExact: " + x + " - " + y + " = " + z);
z = Math.multiplyExact(x, y);
System.out.println("multiplyExact: " + x + " * " + y + " = " + z);
z = Math.incrementExact(x);
System.out.println("incrementExact: " + x + " + 1 = " + z);
z = Math.decrementExact(y);
System.out.println("decrementExact: " + y + " - 1 = " + z);
z = Math.negateExact(x);
System.out.println("negateExact: " + x + " * -1 = " + z);
}
}
输出:
addExact: 10000 + 10000 = 20000
subtractExact: 10000 - 10000 = 0
multiplyExact: 10000 * 10000 = 100000000
incrementExact: 10000 + 1 = 10001
decrementExact: 10000 - 1 = 9999
negateExact: 10000 * -1 = -10000
本文介绍了Java8中Math类新增的精确运算方法,如addExact、subtractExact、multiplyExact等,这些方法在运算结果溢出时会抛出ArithmeticException,帮助开发者更早地发现并处理溢出问题。
2894

被折叠的 条评论
为什么被折叠?



