forked from winterbe/java8-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMath1.java
More file actions
58 lines (47 loc) · 1.45 KB
/
Math1.java
File metadata and controls
58 lines (47 loc) · 1.45 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.winterbe.java8.samples.misc;
/**
* @author Benjamin Winterberg
*/
public class Math1 {
public static void main(String[] args) {
testMathExact();
testUnsignedInt();
}
private static void testUnsignedInt() {
try {
Integer.parseUnsignedInt("-123", 10);
}
catch (NumberFormatException e) {
System.out.println(e.getMessage());
}
long maxUnsignedInt = (1l << 32) - 1;
System.out.println(maxUnsignedInt);
String string = String.valueOf(maxUnsignedInt);
int unsignedInt = Integer.parseUnsignedInt(string, 10);
System.out.println(unsignedInt);
String string2 = Integer.toUnsignedString(unsignedInt, 10);
System.out.println(string2);
try {
Integer.parseInt(string, 10);
}
catch (NumberFormatException e) {
System.err.println("could not parse signed int of " + maxUnsignedInt);
}
}
private static void testMathExact() {
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MAX_VALUE + 1);
try {
Math.addExact(Integer.MAX_VALUE, 1);
}
catch (ArithmeticException e) {
System.err.println(e.getMessage());
}
try {
Math.toIntExact(Long.MAX_VALUE);
}
catch (ArithmeticException e) {
System.err.println(e.getMessage());
}
}
}