Skip to content

Commit eeb09a6

Browse files
committed
Partial update to JDK 17 Features
1 parent 3a84d49 commit eeb09a6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+822
-119
lines changed

collections/BasicRecord.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// collections/BasicRecord.java
2+
// (c)2021 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
// {NewFeature} Since JDK 16
6+
import java.util.*;
7+
8+
record Employee(String name, int id) {}
9+
10+
public class BasicRecord {
11+
public static void main(String[] args) {
12+
var bob = new Employee("Bob Dobbs", 11);
13+
var dot = new Employee("Dorothy Gale", 9);
14+
// bob.id = 12; // Error:
15+
// id has private access in Employee
16+
System.out.println(bob.name()); // Accessor
17+
System.out.println(bob.id()); // Accessor
18+
System.out.println(bob); // toString()
19+
// Employee works as the key in a Map:
20+
var map = Map.of(bob, "A", dot, "B");
21+
System.out.println(map);
22+
}
23+
}
24+
/* Output:
25+
Bob Dobbs
26+
11
27+
Employee[name=Bob Dobbs, id=11]
28+
{Employee[name=Dorothy Gale, id=9]=B, Employee[name=Bob Dobbs, id=11]=A}
29+
*/

collections/CompactConstructor.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// collections/CompactConstructor.java
2+
// (c)2021 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
// {NewFeature} Since JDK 16
6+
7+
record Point(int x, int y) {
8+
void assertPositive(int val) {
9+
if(val < 0)
10+
throw new IllegalArgumentException("negative");
11+
}
12+
Point { // Compact: No parameter list
13+
assertPositive(x);
14+
assertPositive(y);
15+
}
16+
}

collections/ComposedRecord.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// collections/ComposedRecord.java
2+
// (c)2021 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
// {NewFeature} Since JDK 16
6+
7+
record Company(Employee[] e) {}
8+
9+
// class Conglomerate extends Company {}
10+
// error: cannot inherit from final Company

collections/CopyRecord.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// collections/CopyRecord.java
2+
// (c)2021 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
// {NewFeature} Since JDK 16
6+
7+
record R(int a, double b, char c) {}
8+
9+
public class CopyRecord {
10+
public static void main(String[] args) {
11+
var r1 = new R(11, 2.2, 'z');
12+
var r2 = new R(r1.a(), r1.b(), r1.c());
13+
System.out.println(r1.equals(r2));
14+
}
15+
}
16+
/* Output:
17+
true
18+
*/

collections/FinalFields.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// collections/FinalFields.java
2+
// (c)2021 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
// {NewFeature} Since JDK 16
6+
import java.util.*;
7+
8+
record FinalFields(int i) {
9+
int timesTen() { return i * 10; }
10+
// void tryToChange() { i++; } // Error:
11+
// cannot assign a value to final variable i
12+
}

collections/GenericTypeInference.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// collections/GenericTypeInference.java
2+
// (c)2021 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
// {NewFeature} Since JDK 11
6+
import java.util.*;
7+
8+
public class GenericTypeInference {
9+
void old() {
10+
ArrayList<Apple> apples = new ArrayList<>();
11+
}
12+
void modern() {
13+
var apples = new ArrayList<Apple>();
14+
}
15+
void pitFall() {
16+
var apples = new ArrayList<>();
17+
apples.add(new Apple());
18+
apples.get(0); // Comes back as plain Object
19+
}
20+
}

collections/ImplementingRecord.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// collections/ImplementingRecord.java
2+
// (c)2021 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
// {NewFeature} Since JDK 16
6+
7+
interface Star {
8+
double brightness();
9+
double density();
10+
}
11+
12+
record RedDwarf(double brightness) implements Star {
13+
@Override public double density() { return 100.0; }
14+
}

collections/NestedLocalRecords.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// collections/NestedLocalRecords.java
2+
// (c)2021 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
// {NewFeature} Since JDK 16
6+
7+
public class NestedLocalRecords {
8+
record Nested(String s) {}
9+
void method() {
10+
record Local(String s) {}
11+
}
12+
}

collections/NormalConstructor.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// collections/NormalConstructor.java
2+
// (c)2021 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
// {NewFeature} Since JDK 16
6+
7+
record Value(int x) {
8+
Value(int x) { // With the parameter list
9+
this.x = x; // Must explicitly initialize
10+
}
11+
}

collections/PlusTen.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// collections/PlusTen.java
2+
// (c)2021 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
// {NewFeature} Since JDK 16
6+
7+
record PlusTen(int x) {
8+
PlusTen {
9+
x += 10;
10+
}
11+
// Adjustment to field can only happen in
12+
// the constructor. Still not legal:
13+
// void mutate() { x += 10; }
14+
public static void main(String[] args) {
15+
System.out.println(new PlusTen(10));
16+
}
17+
}
18+
/* Output:
19+
PlusTen[x=20]
20+
*/

concurrent/PSP2.txt

Lines changed: 148 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
0: main
2-
1: main
2+
1: ForkJoinPool.commonPool-worker-2
33
2: main
44
3: main
55
4: main
@@ -107,22 +107,150 @@
107107
106: main
108108
107: main
109109
108: main
110-
109: main
111-
110: main
112-
111: main
113-
112: main
114-
113: main
115-
114: main
116-
115: main
117-
116: main
118-
117: main
119-
118: main
120-
119: main
121-
120: main
122-
121: main
123-
122: main
124-
123: main
125-
124: main
126-
125: main
127-
126: main
128-
127: main
110+
108: ForkJoinPool.commonPool-worker-2
111+
109: ForkJoinPool.commonPool-worker-2
112+
110: ForkJoinPool.commonPool-worker-2
113+
111: ForkJoinPool.commonPool-worker-2
114+
112: ForkJoinPool.commonPool-worker-2
115+
113: ForkJoinPool.commonPool-worker-2
116+
114: ForkJoinPool.commonPool-worker-2
117+
115: ForkJoinPool.commonPool-worker-2
118+
116: ForkJoinPool.commonPool-worker-2
119+
117: ForkJoinPool.commonPool-worker-2
120+
118: ForkJoinPool.commonPool-worker-2
121+
119: ForkJoinPool.commonPool-worker-2
122+
120: ForkJoinPool.commonPool-worker-2
123+
121: ForkJoinPool.commonPool-worker-2
124+
122: ForkJoinPool.commonPool-worker-2
125+
123: ForkJoinPool.commonPool-worker-2
126+
124: ForkJoinPool.commonPool-worker-2
127+
125: ForkJoinPool.commonPool-worker-2
128+
126: ForkJoinPool.commonPool-worker-2
129+
127: ForkJoinPool.commonPool-worker-2
130+
128: ForkJoinPool.commonPool-worker-2
131+
129: ForkJoinPool.commonPool-worker-2
132+
130: ForkJoinPool.commonPool-worker-2
133+
131: ForkJoinPool.commonPool-worker-2
134+
132: ForkJoinPool.commonPool-worker-2
135+
133: ForkJoinPool.commonPool-worker-2
136+
134: ForkJoinPool.commonPool-worker-2
137+
135: ForkJoinPool.commonPool-worker-2
138+
136: ForkJoinPool.commonPool-worker-2
139+
137: ForkJoinPool.commonPool-worker-2
140+
138: ForkJoinPool.commonPool-worker-2
141+
139: ForkJoinPool.commonPool-worker-2
142+
140: ForkJoinPool.commonPool-worker-2
143+
141: ForkJoinPool.commonPool-worker-2
144+
142: ForkJoinPool.commonPool-worker-2
145+
143: ForkJoinPool.commonPool-worker-2
146+
144: ForkJoinPool.commonPool-worker-2
147+
145: ForkJoinPool.commonPool-worker-2
148+
146: ForkJoinPool.commonPool-worker-2
149+
147: ForkJoinPool.commonPool-worker-2
150+
148: ForkJoinPool.commonPool-worker-2
151+
149: ForkJoinPool.commonPool-worker-2
152+
150: ForkJoinPool.commonPool-worker-2
153+
151: ForkJoinPool.commonPool-worker-2
154+
152: ForkJoinPool.commonPool-worker-2
155+
153: ForkJoinPool.commonPool-worker-2
156+
154: ForkJoinPool.commonPool-worker-2
157+
155: ForkJoinPool.commonPool-worker-2
158+
156: ForkJoinPool.commonPool-worker-2
159+
157: ForkJoinPool.commonPool-worker-2
160+
158: ForkJoinPool.commonPool-worker-2
161+
159: ForkJoinPool.commonPool-worker-2
162+
160: ForkJoinPool.commonPool-worker-2
163+
161: ForkJoinPool.commonPool-worker-2
164+
162: ForkJoinPool.commonPool-worker-2
165+
163: ForkJoinPool.commonPool-worker-2
166+
164: ForkJoinPool.commonPool-worker-2
167+
165: ForkJoinPool.commonPool-worker-2
168+
166: ForkJoinPool.commonPool-worker-2
169+
167: ForkJoinPool.commonPool-worker-2
170+
168: ForkJoinPool.commonPool-worker-2
171+
169: ForkJoinPool.commonPool-worker-2
172+
170: ForkJoinPool.commonPool-worker-2
173+
171: ForkJoinPool.commonPool-worker-2
174+
172: ForkJoinPool.commonPool-worker-2
175+
173: ForkJoinPool.commonPool-worker-2
176+
174: ForkJoinPool.commonPool-worker-2
177+
175: ForkJoinPool.commonPool-worker-2
178+
176: ForkJoinPool.commonPool-worker-2
179+
177: ForkJoinPool.commonPool-worker-2
180+
178: ForkJoinPool.commonPool-worker-2
181+
179: ForkJoinPool.commonPool-worker-2
182+
180: ForkJoinPool.commonPool-worker-2
183+
181: ForkJoinPool.commonPool-worker-2
184+
182: ForkJoinPool.commonPool-worker-2
185+
183: ForkJoinPool.commonPool-worker-2
186+
184: ForkJoinPool.commonPool-worker-2
187+
185: ForkJoinPool.commonPool-worker-2
188+
186: ForkJoinPool.commonPool-worker-2
189+
187: ForkJoinPool.commonPool-worker-2
190+
188: ForkJoinPool.commonPool-worker-2
191+
189: ForkJoinPool.commonPool-worker-2
192+
190: ForkJoinPool.commonPool-worker-2
193+
191: ForkJoinPool.commonPool-worker-2
194+
192: ForkJoinPool.commonPool-worker-2
195+
193: ForkJoinPool.commonPool-worker-2
196+
194: ForkJoinPool.commonPool-worker-2
197+
195: ForkJoinPool.commonPool-worker-2
198+
196: ForkJoinPool.commonPool-worker-2
199+
197: ForkJoinPool.commonPool-worker-2
200+
198: ForkJoinPool.commonPool-worker-2
201+
199: ForkJoinPool.commonPool-worker-2
202+
200: ForkJoinPool.commonPool-worker-2
203+
201: ForkJoinPool.commonPool-worker-2
204+
202: ForkJoinPool.commonPool-worker-2
205+
203: ForkJoinPool.commonPool-worker-2
206+
204: ForkJoinPool.commonPool-worker-2
207+
205: ForkJoinPool.commonPool-worker-2
208+
206: ForkJoinPool.commonPool-worker-2
209+
207: ForkJoinPool.commonPool-worker-2
210+
208: ForkJoinPool.commonPool-worker-2
211+
209: ForkJoinPool.commonPool-worker-2
212+
210: ForkJoinPool.commonPool-worker-2
213+
211: ForkJoinPool.commonPool-worker-2
214+
212: ForkJoinPool.commonPool-worker-2
215+
213: ForkJoinPool.commonPool-worker-2
216+
214: ForkJoinPool.commonPool-worker-2
217+
215: ForkJoinPool.commonPool-worker-2
218+
216: ForkJoinPool.commonPool-worker-2
219+
217: ForkJoinPool.commonPool-worker-2
220+
218: ForkJoinPool.commonPool-worker-2
221+
219: ForkJoinPool.commonPool-worker-2
222+
220: ForkJoinPool.commonPool-worker-2
223+
221: ForkJoinPool.commonPool-worker-2
224+
222: ForkJoinPool.commonPool-worker-2
225+
223: ForkJoinPool.commonPool-worker-2
226+
224: ForkJoinPool.commonPool-worker-2
227+
225: ForkJoinPool.commonPool-worker-2
228+
226: ForkJoinPool.commonPool-worker-2
229+
227: ForkJoinPool.commonPool-worker-2
230+
228: ForkJoinPool.commonPool-worker-2
231+
229: ForkJoinPool.commonPool-worker-2
232+
230: ForkJoinPool.commonPool-worker-2
233+
231: ForkJoinPool.commonPool-worker-2
234+
232: ForkJoinPool.commonPool-worker-2
235+
233: ForkJoinPool.commonPool-worker-2
236+
234: ForkJoinPool.commonPool-worker-2
237+
236: main
238+
237: main
239+
238: main
240+
239: main
241+
240: main
242+
241: main
243+
242: main
244+
243: main
245+
244: main
246+
245: main
247+
246: main
248+
247: main
249+
248: main
250+
249: main
251+
250: main
252+
251: main
253+
252: main
254+
253: main
255+
254: main
256+
255: main
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// exceptions/BetterNullPointerReports.java
2+
// (c)2021 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
// {NewFeature} Since JDK 15
6+
7+
class A {
8+
String s;
9+
A(String s) {
10+
this.s = s;
11+
}
12+
}
13+
14+
class B {
15+
A a;
16+
B(A a) {
17+
this.a = a;
18+
}
19+
}
20+
21+
class C {
22+
B b;
23+
C(B b) {
24+
this.b = b;
25+
}
26+
}
27+
28+
public class BetterNullPointerReports {
29+
public static void main(String[] args) {
30+
C[] ca = {
31+
new C(new B(new A(null))),
32+
new C(new B(null)),
33+
new C(null),
34+
};
35+
for(C c: ca) {
36+
try {
37+
System.out.println(c.b.a.s);
38+
} catch(NullPointerException npe) {
39+
System.out.println(npe);
40+
}
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)