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+
*/
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+
}
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+
}
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+
}
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+
*/

0 commit comments

Comments
 (0)