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
26 lines (18 loc) · 883 Bytes
/
Main.java
File metadata and controls
26 lines (18 loc) · 883 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
package modern.challenge;
public class Main {
public static void main(String[] args) {
long nrLong = Integer.MAX_VALUE;
long nrMaxLong = Long.MAX_VALUE;
int intNrCast = (int) nrLong; // Ok
int intNrMaxCast = (int) nrMaxLong; // Not ok
System.out.println("Cast Integer.MAX_VALUE: " + intNrCast);
System.out.println("Cast Long.MAX_VALUE: " + intNrMaxCast);
int intNrValue = Long.valueOf(nrLong).intValue(); // Ok
int intNrMaxValue = Long.valueOf(nrMaxLong).intValue(); // Not ok
System.out.println();
System.out.println("intValue() Integer.MAX_VALUE: " + intNrValue);
System.out.println("intValue() Long.MAX_VALUE: " + intNrMaxValue);
int intNrExact = Math.toIntExact(nrLong); // Ok
int intNrMaxExact = Math.toIntExact(nrMaxLong); // ArithmeticException
}
}