forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
27 lines (19 loc) · 817 Bytes
/
Copy pathMain.java
File metadata and controls
27 lines (19 loc) · 817 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package modern.challenge;
import java.util.function.BinaryOperator;
public class Main {
public static void main(String[] args) {
int x = Integer.MAX_VALUE;
int y = Integer.MAX_VALUE;
int z = x + y;
System.out.println(x + " + " + y + " via '+' operator is: " + z);
int zSum = Integer.sum(x, y);
System.out.println(x + " + " + y + " via Integer.sum() is: " + zSum);
// throw ArithmeticException
int zExact = Math.addExact(x, y);
System.out.println(x + " + " + y + " via Math.addExact() is: " + zExact);
// throw ArithmeticException
BinaryOperator<Integer> operator = Math::addExact;
int zExactBo = operator.apply(x, y);
System.out.println(x + " + " + y + " via BinaryOperator is: " + zExactBo);
}
}