Partial update to JDK 17 Features

This commit is contained in:
Bruce Eckel 2021-09-20 17:33:35 -06:00
parent 3a84d492f9
commit eeb09a6997
45 changed files with 822 additions and 119 deletions

View File

@ -0,0 +1,29 @@
// collections/BasicRecord.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 16
import java.util.*;
record Employee(String name, int id) {}
public class BasicRecord {
public static void main(String[] args) {
var bob = new Employee("Bob Dobbs", 11);
var dot = new Employee("Dorothy Gale", 9);
// bob.id = 12; // Error:
// id has private access in Employee
System.out.println(bob.name()); // Accessor
System.out.println(bob.id()); // Accessor
System.out.println(bob); // toString()
// Employee works as the key in a Map:
var map = Map.of(bob, "A", dot, "B");
System.out.println(map);
}
}
/* Output:
Bob Dobbs
11
Employee[name=Bob Dobbs, id=11]
{Employee[name=Dorothy Gale, id=9]=B, Employee[name=Bob Dobbs, id=11]=A}
*/

View File

@ -0,0 +1,16 @@
// collections/CompactConstructor.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 16
record Point(int x, int y) {
void assertPositive(int val) {
if(val < 0)
throw new IllegalArgumentException("negative");
}
Point { // Compact: No parameter list
assertPositive(x);
assertPositive(y);
}
}

View File

@ -0,0 +1,10 @@
// collections/ComposedRecord.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 16
record Company(Employee[] e) {}
// class Conglomerate extends Company {}
// error: cannot inherit from final Company

View File

@ -0,0 +1,18 @@
// collections/CopyRecord.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 16
record R(int a, double b, char c) {}
public class CopyRecord {
public static void main(String[] args) {
var r1 = new R(11, 2.2, 'z');
var r2 = new R(r1.a(), r1.b(), r1.c());
System.out.println(r1.equals(r2));
}
}
/* Output:
true
*/

View File

@ -0,0 +1,12 @@
// collections/FinalFields.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 16
import java.util.*;
record FinalFields(int i) {
int timesTen() { return i * 10; }
// void tryToChange() { i++; } // Error:
// cannot assign a value to final variable i
}

View File

@ -0,0 +1,20 @@
// collections/GenericTypeInference.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 11
import java.util.*;
public class GenericTypeInference {
void old() {
ArrayList<Apple> apples = new ArrayList<>();
}
void modern() {
var apples = new ArrayList<Apple>();
}
void pitFall() {
var apples = new ArrayList<>();
apples.add(new Apple());
apples.get(0); // Comes back as plain Object
}
}

View File

@ -0,0 +1,14 @@
// collections/ImplementingRecord.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 16
interface Star {
double brightness();
double density();
}
record RedDwarf(double brightness) implements Star {
@Override public double density() { return 100.0; }
}

View File

@ -0,0 +1,12 @@
// collections/NestedLocalRecords.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 16
public class NestedLocalRecords {
record Nested(String s) {}
void method() {
record Local(String s) {}
}
}

View File

@ -0,0 +1,11 @@
// collections/NormalConstructor.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 16
record Value(int x) {
Value(int x) { // With the parameter list
this.x = x; // Must explicitly initialize
}
}

20
collections/PlusTen.java Normal file
View File

@ -0,0 +1,20 @@
// collections/PlusTen.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 16
record PlusTen(int x) {
PlusTen {
x += 10;
}
// Adjustment to field can only happen in
// the constructor. Still not legal:
// void mutate() { x += 10; }
public static void main(String[] args) {
System.out.println(new PlusTen(10));
}
}
/* Output:
PlusTen[x=20]
*/

View File

@ -1,5 +1,5 @@
0: main
1: main
1: ForkJoinPool.commonPool-worker-2
2: main
3: main
4: main
@ -107,22 +107,150 @@
106: main
107: main
108: main
109: main
110: main
111: main
112: main
113: main
114: main
115: main
116: main
117: main
118: main
119: main
120: main
121: main
122: main
123: main
124: main
125: main
126: main
127: main
108: ForkJoinPool.commonPool-worker-2
109: ForkJoinPool.commonPool-worker-2
110: ForkJoinPool.commonPool-worker-2
111: ForkJoinPool.commonPool-worker-2
112: ForkJoinPool.commonPool-worker-2
113: ForkJoinPool.commonPool-worker-2
114: ForkJoinPool.commonPool-worker-2
115: ForkJoinPool.commonPool-worker-2
116: ForkJoinPool.commonPool-worker-2
117: ForkJoinPool.commonPool-worker-2
118: ForkJoinPool.commonPool-worker-2
119: ForkJoinPool.commonPool-worker-2
120: ForkJoinPool.commonPool-worker-2
121: ForkJoinPool.commonPool-worker-2
122: ForkJoinPool.commonPool-worker-2
123: ForkJoinPool.commonPool-worker-2
124: ForkJoinPool.commonPool-worker-2
125: ForkJoinPool.commonPool-worker-2
126: ForkJoinPool.commonPool-worker-2
127: ForkJoinPool.commonPool-worker-2
128: ForkJoinPool.commonPool-worker-2
129: ForkJoinPool.commonPool-worker-2
130: ForkJoinPool.commonPool-worker-2
131: ForkJoinPool.commonPool-worker-2
132: ForkJoinPool.commonPool-worker-2
133: ForkJoinPool.commonPool-worker-2
134: ForkJoinPool.commonPool-worker-2
135: ForkJoinPool.commonPool-worker-2
136: ForkJoinPool.commonPool-worker-2
137: ForkJoinPool.commonPool-worker-2
138: ForkJoinPool.commonPool-worker-2
139: ForkJoinPool.commonPool-worker-2
140: ForkJoinPool.commonPool-worker-2
141: ForkJoinPool.commonPool-worker-2
142: ForkJoinPool.commonPool-worker-2
143: ForkJoinPool.commonPool-worker-2
144: ForkJoinPool.commonPool-worker-2
145: ForkJoinPool.commonPool-worker-2
146: ForkJoinPool.commonPool-worker-2
147: ForkJoinPool.commonPool-worker-2
148: ForkJoinPool.commonPool-worker-2
149: ForkJoinPool.commonPool-worker-2
150: ForkJoinPool.commonPool-worker-2
151: ForkJoinPool.commonPool-worker-2
152: ForkJoinPool.commonPool-worker-2
153: ForkJoinPool.commonPool-worker-2
154: ForkJoinPool.commonPool-worker-2
155: ForkJoinPool.commonPool-worker-2
156: ForkJoinPool.commonPool-worker-2
157: ForkJoinPool.commonPool-worker-2
158: ForkJoinPool.commonPool-worker-2
159: ForkJoinPool.commonPool-worker-2
160: ForkJoinPool.commonPool-worker-2
161: ForkJoinPool.commonPool-worker-2
162: ForkJoinPool.commonPool-worker-2
163: ForkJoinPool.commonPool-worker-2
164: ForkJoinPool.commonPool-worker-2
165: ForkJoinPool.commonPool-worker-2
166: ForkJoinPool.commonPool-worker-2
167: ForkJoinPool.commonPool-worker-2
168: ForkJoinPool.commonPool-worker-2
169: ForkJoinPool.commonPool-worker-2
170: ForkJoinPool.commonPool-worker-2
171: ForkJoinPool.commonPool-worker-2
172: ForkJoinPool.commonPool-worker-2
173: ForkJoinPool.commonPool-worker-2
174: ForkJoinPool.commonPool-worker-2
175: ForkJoinPool.commonPool-worker-2
176: ForkJoinPool.commonPool-worker-2
177: ForkJoinPool.commonPool-worker-2
178: ForkJoinPool.commonPool-worker-2
179: ForkJoinPool.commonPool-worker-2
180: ForkJoinPool.commonPool-worker-2
181: ForkJoinPool.commonPool-worker-2
182: ForkJoinPool.commonPool-worker-2
183: ForkJoinPool.commonPool-worker-2
184: ForkJoinPool.commonPool-worker-2
185: ForkJoinPool.commonPool-worker-2
186: ForkJoinPool.commonPool-worker-2
187: ForkJoinPool.commonPool-worker-2
188: ForkJoinPool.commonPool-worker-2
189: ForkJoinPool.commonPool-worker-2
190: ForkJoinPool.commonPool-worker-2
191: ForkJoinPool.commonPool-worker-2
192: ForkJoinPool.commonPool-worker-2
193: ForkJoinPool.commonPool-worker-2
194: ForkJoinPool.commonPool-worker-2
195: ForkJoinPool.commonPool-worker-2
196: ForkJoinPool.commonPool-worker-2
197: ForkJoinPool.commonPool-worker-2
198: ForkJoinPool.commonPool-worker-2
199: ForkJoinPool.commonPool-worker-2
200: ForkJoinPool.commonPool-worker-2
201: ForkJoinPool.commonPool-worker-2
202: ForkJoinPool.commonPool-worker-2
203: ForkJoinPool.commonPool-worker-2
204: ForkJoinPool.commonPool-worker-2
205: ForkJoinPool.commonPool-worker-2
206: ForkJoinPool.commonPool-worker-2
207: ForkJoinPool.commonPool-worker-2
208: ForkJoinPool.commonPool-worker-2
209: ForkJoinPool.commonPool-worker-2
210: ForkJoinPool.commonPool-worker-2
211: ForkJoinPool.commonPool-worker-2
212: ForkJoinPool.commonPool-worker-2
213: ForkJoinPool.commonPool-worker-2
214: ForkJoinPool.commonPool-worker-2
215: ForkJoinPool.commonPool-worker-2
216: ForkJoinPool.commonPool-worker-2
217: ForkJoinPool.commonPool-worker-2
218: ForkJoinPool.commonPool-worker-2
219: ForkJoinPool.commonPool-worker-2
220: ForkJoinPool.commonPool-worker-2
221: ForkJoinPool.commonPool-worker-2
222: ForkJoinPool.commonPool-worker-2
223: ForkJoinPool.commonPool-worker-2
224: ForkJoinPool.commonPool-worker-2
225: ForkJoinPool.commonPool-worker-2
226: ForkJoinPool.commonPool-worker-2
227: ForkJoinPool.commonPool-worker-2
228: ForkJoinPool.commonPool-worker-2
229: ForkJoinPool.commonPool-worker-2
230: ForkJoinPool.commonPool-worker-2
231: ForkJoinPool.commonPool-worker-2
232: ForkJoinPool.commonPool-worker-2
233: ForkJoinPool.commonPool-worker-2
234: ForkJoinPool.commonPool-worker-2
236: main
237: main
238: main
239: main
240: main
241: main
242: main
243: main
244: main
245: main
246: main
247: main
248: main
249: main
250: main
251: main
252: main
253: main
254: main
255: main

View File

@ -0,0 +1,43 @@
// exceptions/BetterNullPointerReports.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 15
class A {
String s;
A(String s) {
this.s = s;
}
}
class B {
A a;
B(A a) {
this.a = a;
}
}
class C {
B b;
C(B b) {
this.b = b;
}
}
public class BetterNullPointerReports {
public static void main(String[] args) {
C[] ca = {
new C(new B(new A(null))),
new C(new B(null)),
new C(null),
};
for(C c: ca) {
try {
System.out.println(c.b.a.s);
} catch(NullPointerException npe) {
System.out.println(npe);
}
}
}
}

View File

@ -0,0 +1,48 @@
// exceptions/EffectivelyFinalTWR.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 9
import java.io.*;
public class EffectivelyFinalTWR {
static void old() {
try (
InputStream r1 = new FileInputStream(
new File("TryWithResources.java"));
InputStream r2 = new FileInputStream(
new File("EffectivelyFinalTWR.java"));
) {
r1.read();
r2.read();
} catch(IOException e) {
// Handle exceptions
}
}
static void jdk9() throws IOException {
final InputStream r1 = new FileInputStream(
new File("TryWithResources.java"));
// Effectively final:
InputStream r2 = new FileInputStream(
new File("EffectivelyFinalTWR.java"));
try (r1; r2) {
r1.read();
r2.read();
}
// r1 and r2 are still in scope. Accessing
// either one throws an exception:
r1.read();
r2.read();
}
public static void main(String[] args) {
old();
try {
jdk9();
} catch(IOException e) {
System.out.println(e);
}
}
}
/* Output:
java.io.IOException: Stream Closed
*/

View File

@ -13,7 +13,7 @@ public class MessyExceptions {
int contents = in.read();
// Process contents
} catch(IOException e) {
// Handle the error
// Handle errors
} finally {
if(in != null) {
try {

View File

@ -13,7 +13,7 @@ public class TryWithResources {
int contents = in.read();
// Process contents
} catch(IOException e) {
// Handle the error
// Handle errors
}
}
}

View File

@ -7,14 +7,8 @@ import java.util.*;
public class ArrayInit {
public static void main(String[] args) {
Integer[] a = {
1, 2,
3, // Autoboxing
};
Integer[] b = new Integer[]{
1, 2,
3, // Autoboxing
};
Integer[] a = { 1, 2, 3, };
Integer[] b = new Integer[]{ 1, 2, 3, };
System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(b));
}

View File

@ -0,0 +1,19 @@
// housekeeping/ForTypeInference.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 11
public class ForTypeInference {
public static void main(String[] args) {
for(var s : Spiciness.values())
System.out.println(s);
}
}
/* Ouput:
NOT
MILD
MEDIUM
HOT
FLAMING
*/

View File

@ -2,7 +2,7 @@
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Using array syntax to create variable argument lists
// Using ellipses to define a variable argument list
public class NewVarArgs {
static void printArray(Object... args) {

View File

@ -101,20 +101,12 @@ public class PrimitiveOverloading {
}
}
/* Output:
5: f1(int) f2(int) f3(int) f4(int) f5(long) f6(float)
f7(double)
char: f1(char) f2(int) f3(int) f4(int) f5(long)
f6(float) f7(double)
byte: f1(byte) f2(byte) f3(short) f4(int) f5(long)
f6(float) f7(double)
short: f1(short) f2(short) f3(short) f4(int) f5(long)
f6(float) f7(double)
int: f1(int) f2(int) f3(int) f4(int) f5(long) f6(float)
f7(double)
long: f1(long) f2(long) f3(long) f4(long) f5(long)
f6(float) f7(double)
float: f1(float) f2(float) f3(float) f4(float)
f5(float) f6(float) f7(double)
double: f1(double) f2(double) f3(double) f4(double)
f5(double) f6(double) f7(double)
5: f1(int) f2(int) f3(int) f4(int) f5(long) f6(float) f7(double)
char: f1(char) f2(int) f3(int) f4(int) f5(long) f6(float) f7(double)
byte: f1(byte) f2(byte) f3(short) f4(int) f5(long) f6(float) f7(double)
short: f1(short) f2(short) f3(short) f4(int) f5(long) f6(float) f7(double)
int: f1(int) f2(int) f3(int) f4(int) f5(long) f6(float) f7(double)
long: f1(long) f2(long) f3(long) f4(long) f5(long) f6(float) f7(double)
float: f1(float) f2(float) f3(float) f4(float) f5(float) f6(float) f7(double)
double: f1(double) f2(double) f3(double) f4(double) f5(double) f6(double) f7(double)
*/

View File

@ -0,0 +1,36 @@
// housekeeping/TypeInference.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 11
class Plumbus {}
public class TypeInference {
void method() {
// Explicit type:
String hello1 = "Hello";
// Type inference:
var hello = "Hello!";
// Works for user-defined types:
Plumbus pb1 = new Plumbus();
var pb2 = new Plumbus();
}
// Also works for static methods:
static void staticMethod() {
var hello = "Hello!";
var pb2 = new Plumbus();
}
}
class NoInference {
String field1 = "Field initialization";
// var field2 = "Can't do this";
// void method() {
// var noInitializer; // No inference data
// var aNull = null; // No inference data
// }
// var inferReturnType() {
// return "Can't infer return type";
// }
}

View File

@ -13,8 +13,7 @@ public class VarArgs {
System.out.println();
}
public static void main(String[] args) {
printArray(new Object[]{
47, (float) 3.14, 11.11});
printArray(new Object[]{47, (float) 3.14, 11.11});
printArray(new Object[]{"one", "two", "three" });
printArray(new Object[]{new A(), new A(), new A()});
}

View File

@ -0,0 +1,19 @@
// interfaces/CheckedDowncast.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 17
sealed interface II permits JJ {}
final class JJ implements II {}
class Something {}
public class CheckedDowncast {
public void f() {
II i = new JJ();
JJ j = (JJ)i;
// Something s = (Something)i;
// error: incompatible types: II cannot
// be converted to Something
}
}

View File

@ -37,7 +37,7 @@ interface Sam2 {
}
}
// This works because the argument lists are distinct:
// Works because the argument lists are distinct:
class Sam implements Sam1, Sam2 {}
interface Max1 {

View File

@ -1,36 +0,0 @@
// interfaces/Machine.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.util.*;
import onjava.Operations;
class Bing implements Operations {
@Override public void execute() {
Operations.show("Bing");
}
}
class Crack implements Operations {
@Override public void execute() {
Operations.show("Crack");
}
}
class Twist implements Operations {
@Override public void execute() {
Operations.show("Twist");
}
}
public class Machine {
public static void main(String[] args) {
Operations.runOps(
new Bing(), new Crack(), new Twist());
}
}
/* Output:
Bing
Crack
Twist
*/

39
interfaces/MetalWork.java Normal file
View File

@ -0,0 +1,39 @@
// interfaces/MetalWork.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import onjava.Operation;
class Heat implements Operation {
@Override public void execute() {
Operation.show("Heat");
}
}
public class MetalWork {
public static void main(String[] args) {
// Must be defined in a static context
// to access a method reference:
Operation twist = new Operation() {
public void execute() {
Operation.show("Twist");
}
};
Operation.runOps(
new Heat(), // [1]
new Operation() { // [2]
public void execute() {
Operation.show("Hammer");
}
},
twist::execute, // [3]
() -> Operation.show("Anneal") // [4]
);
}
}
/* Output:
Heat
Hammer
Twist
Anneal
*/

11
interfaces/NonSealed.java Normal file
View File

@ -0,0 +1,11 @@
// interfaces/NonSealed.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 17
sealed class Super permits Sub1, Sub2 {}
final class Sub1 extends Super {}
non-sealed class Sub2 extends Super {}
class Any1 extends Sub2 {}
class Any2 extends Sub2 {}

View File

@ -0,0 +1,22 @@
// interfaces/PermittedSubclasses.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 17
sealed class Color permits Red, Green, Blue {}
final class Red extends Color {}
final class Green extends Color {}
final class Blue extends Color {}
public class PermittedSubclasses {
public static void main(String[] args) {
for(var p: Color.class.getPermittedSubclasses())
System.out.println(p.getSimpleName());
}
}
/* Output:
Red
Green
Blue
*/

View File

@ -0,0 +1,54 @@
// interfaces/PrivateInterfaceMethods.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 9
interface Old {
default void fd() {
System.out.println("Old::fd()");
}
static void fs() {
System.out.println("Old::fs()");
}
default void f() {
fd();
}
static void g() {
fs();
}
}
class ImplOld implements Old {}
interface JDK9 {
private void fd() { // Automatically default
System.out.println("JDK9::fd()");
}
private static void fs() {
System.out.println("JDK9::fs()");
}
default void f() {
fd();
}
static void g() {
fs();
}
}
class ImplJDK9 implements JDK9 {}
public class PrivateInterfaceMethods {
public static void main(String[] args) {
new ImplOld().f();
Old.g();
new ImplJDK9().f();
JDK9.g();
}
}
/* Output:
Old::fd()
Old::fs()
JDK9::fd()
JDK9::fs()
*/

9
interfaces/SameFile.java Normal file
View File

@ -0,0 +1,9 @@
// interfaces/SameFile.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 17
sealed class Shape {}
final class Circle extends Shape {}
final class Triangle extends Shape {}

12
interfaces/Sealed.java Normal file
View File

@ -0,0 +1,12 @@
// interfaces/Sealed.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 17
sealed class Base permits D1, D2 {}
final class D1 extends Base {}
final class D2 extends Base {}
// Illegal:
// final class D3 extends Base {}

View File

@ -0,0 +1,6 @@
// interfaces/SealedCat.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 17
final class Cat extends Pet {}

View File

@ -0,0 +1,6 @@
// interfaces/SealedDog.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 17
final class Dog extends Pet {}

View File

@ -0,0 +1,12 @@
// interfaces/SealedInterface.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 17
sealed interface Ifc permits Imp1, Imp2 {}
final class Imp1 implements Ifc {}
final class Imp2 implements Ifc {}
sealed abstract class AC permits X {}
final class X extends AC {}

View File

@ -0,0 +1,6 @@
// interfaces/SealedPets.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 17
sealed class Pet permits Dog, Cat {}

View File

@ -0,0 +1,12 @@
// interfaces/SealedRecords.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 17
sealed interface Employee
permits CLevel, Programmer {}
record CLevel(String type)
implements Employee {}
record Programmer(String experience)
implements Employee {}

View File

@ -0,0 +1,10 @@
// interfaces/SealedSubclasses.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 17
sealed class Bottom permits Level1 {}
sealed class Level1 extends Bottom permits Level2 {}
sealed class Level2 extends Level1 permits Level3 {}
final class Level3 extends Level2 {}

View File

@ -1,14 +1,13 @@
// onjava/Operations.java
// onjava/Operation.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
package onjava;
import java.util.*;
public interface Operations {
public interface Operation {
void execute();
static void runOps(Operations... ops) {
for(Operations op : ops)
static void runOps(Operation... ops) {
for(Operation op : ops)
op.execute();
}
static void show(String msg) {

View File

@ -14,11 +14,11 @@ public class Equivalence {
Integer i1 = value; // [1]
Integer i2 = value;
show("Automatic", i1, i2);
// Old way, deprecated in Java 9 and on:
// Old way, deprecated since Java 9:
Integer r1 = new Integer(value); // [2]
Integer r2 = new Integer(value);
show("new Integer()", r1, r2);
// Preferred in Java 9 and on:
// Preferred since Java 9:
Integer v1 = Integer.valueOf(value); // [3]
Integer v2 = Integer.valueOf(value);
show("Integer.valueOf()", v1, v2);

View File

@ -17,7 +17,7 @@ public class Literals {
char c = 0xffff; // max char hex value
System.out.println(
"c: " + Integer.toBinaryString(c));
byte b = 0x7f; // max byte hex value 10101111;
byte b = 0x7f; // max byte hex value 0111111;
System.out.println(
"b: " + Integer.toBinaryString(b));
short s = 0x7fff; // max short hex value

View File

@ -30,8 +30,7 @@ public class URShift {
/* Output:
11111111111111111111111111111111
1111111111111111111111
1111111111111111111111111111111111111111111111111111111
111111111
1111111111111111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111

View File

@ -1,23 +0,0 @@
// streams/Machine2.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.util.*;
import onjava.Operations;
public class Machine2 {
public static void main(String[] args) {
Arrays.stream(new Operations[] {
() -> Operations.show("Bing"),
() -> Operations.show("Crack"),
() -> Operations.show("Twist"),
() -> Operations.show("Pop")
}).forEach(Operations::execute);
}
}
/* Output:
Bing
Crack
Twist
Pop
*/

23
streams/MetalWork2.java Normal file
View File

@ -0,0 +1,23 @@
// streams/MetalWork2.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.util.*;
import onjava.Operation;
public class MetalWork2 {
public static void main(String[] args) {
Arrays.stream(new Operation[] {
() -> Operation.show("Heat"),
() -> Operation.show("Hammer"),
() -> Operation.show("Twist"),
() -> Operation.show("Anneal")
}).forEach(Operation::execute);
}
}
/* Output:
Heat
Hammer
Twist
Anneal
*/

32
strings/DataPoint.java Normal file
View File

@ -0,0 +1,32 @@
// strings/DataPoint.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 15
public class DataPoint {
private String location;
private Double temperature;
public DataPoint(String loc, Double temp) {
location = loc;
temperature = temp;
}
@Override public String toString() {
return """
Location: %s
Temperature: %.2f
""".formatted(location, temperature);
}
public static void main(String[] args) {
var hill = new DataPoint("Hill", 45.2);
var dale = new DataPoint("Dale", 65.2);
System.out.print(hill);
System.out.print(dale);
}
}
/* Output:
Location: Hill
Temperature: 45.20
Location: Dale
Temperature: 65.20
*/

33
strings/Indentation.java Normal file
View File

@ -0,0 +1,33 @@
// strings/Indentation.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 15
public class Indentation {
public static final String NONE = """
XXX
YYY
"""; // No indentation
public static final String TWO = """
XXX
YYY
"""; // Produces indent of 2
public static final String EIGHT = """
XXX
YYY
"""; // Produces indent of 8
public static void main(String[] args) {
System.out.print(NONE);
System.out.print(TWO);
System.out.print(EIGHT);
}
}
/* Output:
XXX
YYY
XXX
YYY
XXX
YYY
*/

37
strings/TextBlocks.java Normal file
View File

@ -0,0 +1,37 @@
// strings/TextBlocks.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {NewFeature} Since JDK 15
// Poem: Antigonish by Hughes Mearns
public class TextBlocks {
public static final String OLD =
"Yesterday, upon the stair,\n" +
"I met a man who wasn't there\n" +
"He wasn't there again today\n" +
"I wish, I wish he'd go away...\n" +
"\n" +
"When I came home last night at three\n" +
"The man was waiting there for me\n" +
"But when I looked around the hall\n" +
"I couldn't see him there at all!\n";
public static final String NEW = """
Yesterday, upon the stair,
I met a man who wasn't there
He wasn't there again today
I wish, I wish he'd go away...
When I came home last night at three
The man was waiting there for me
But when I looked around the hall
I couldn't see him there at all!
""";
public static void main(String[] args) {
System.out.println(OLD.equals(NEW));
}
}
/* Output:
true
*/