Skip to content

Commit 966a33d

Browse files
committed
updated docs;
Java 13 examples;
1 parent 5bd88f4 commit 966a33d

File tree

7 files changed

+239
-5
lines changed

7 files changed

+239
-5
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,25 @@ A project to explore more about the new features of Java 8, 9, ...
1919
* Java 10
2020
* Process API
2121
* Collections improvements
22-
* Switch expression (preview)
22+
* Application Class-Data Sharing
23+
* var
2324

2425
* Java 11
2526
* Http Client (incubator)
2627

2728
* Java 12
28-
* Switch expression (preview 2)
29+
* Switch expression (preview 1)
2930
* Compact Number Format
3031
* Collectors improvements
3132
* CompletableFuture improvements
3233
* CDS enable by default
3334
* New GC and improvements
35+
36+
* Java 13
37+
* Switch expression (preview 2)
38+
* Text blocks
39+
* String API updates
40+
* NIO updates
41+
* Socket and ServerSocket reimplementation (Project Loom)
42+
* Dynamic CDS Archives
43+
* ZGC: Uncommit Unused Memory

java-10/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Java 10
2+
3+
## JEPs
4+
5+
* [310](https://openjdk.java.net/jeps/310) - Application Class-Data Sharing

java-11/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Java 11
2+
3+
## Links
4+
5+
* [Oracle's Z GC](https://wiki.openjdk.java.net/display/zgc/Main)

java-12/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ To run each example use:
3030
* [12](https://openjdk.java.net/jeps/12) - Preview language and VM features
3131
* [325](http://openjdk.java.net/jeps/325) - Switch expressions (preview)
3232
* [341](http://openjdk.java.net/jeps/341) - Default CDS Archives
33-
* [189](http://openjdk.java.net/jeps/189) - Low-Pause-Time GC
33+
* [189](http://openjdk.java.net/jeps/189) - Low-Pause-Time GC (experimental)
3434
* [344](http://openjdk.java.net/jeps/344) - Abortable mixed collections for G1
3535
* [345](http://openjdk.java.net/jeps/346) - Promptly return unused memory from G1
3636
* [334](http://openjdk.java.net/jeps/334) - JVM Constants API
@@ -43,7 +43,7 @@ Tools:
4343
* compiler plugin: 3.8.0
4444
* surefire and failsafe: 2.22.0
4545

46-
```
46+
```xml
4747
<plugin>
4848
<artifactId>maven-compiler-plugin</artifactId>
4949
<configuration>
@@ -69,4 +69,5 @@ Libs which manipulate bytecode that will need update:
6969
## Links
7070

7171
* [Java 12 Guide](https://blog.codefx.org/java/java-12-guide/)
72-
* [Should you adopt Java 12 or stick on Java 11?](https://blog.joda.org/2018/10/adopt-java-12-or-stick-on-11.html)
72+
* [Should you adopt Java 12 or stick on Java 11?](https://blog.joda.org/2018/10/adopt-java-12-or-stick-on-11.html)
73+
* [Shenandoah GC](https://wiki.openjdk.java.net/display/shenandoah/Main)

java-13/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Java 13
2+
3+
To run each example use: `java --enable-preview --source 13 <FileName.java>`
4+
5+
## Features
6+
7+
### Language
8+
9+
* Switch expressions improvements
10+
* Changed `break` from Java 12 to `yield`
11+
* Text blocks
12+
* flag to compile: `-Xlint:text-blocks`
13+
* API improvements
14+
* String methods:
15+
* `stripIdent`: same operation done by the compiler to strip incidental identation from text block';
16+
* `translateEscapes`: same operation done by the compiler to translate escapes in strings;
17+
* `formatted`: shortcut to String.format '`"%s years old".formatted(42)`)
18+
* NIO improvements
19+
* Socket and ServerSocket reimplementation (alignment to [Project Loom](https://openjdk.java.net/projects/loom/))
20+
21+
### JVM
22+
23+
* Creating Class-Data archives For AppCDS
24+
* flag to java `-XX:ArchiveClassesAtExit=<FileHere.jsa>` to generate class-data on exit
25+
* run with it: `-XX:SharedArchiveFile=<FileHere.jsa>`
26+
* ZGC (Oracle's Z GC) returns unused memory to SO
27+
* `-XX:ZUncommitDelay=<seconds>` to set the delay in seconds
28+
* `-XX:SoftMaxHeapSize`: soft (won't generate OutOfMemoryError - will request more) limit to avoid the JVM to grow in memory
29+
30+
## JEPs
31+
32+
* [354](https://openjdk.java.net/jeps/354) - Switch expression (preview 2)
33+
* [355](https://openjdk.java.net/jeps/355) - Text blocks (preview)
34+
* [350](https://openjdk.java.net/jeps/350) - Dynamic CDS Archives
35+
* [351](https://openjdk.java.net/jeps/351) - ZGC: Uncommit Unused Memory
36+
* [353](https://openjdk.java.net/jeps/353) - Reimplement the Legacy Socket API
37+
38+
## Warnings
39+
40+
* Tools
41+
* Maven: 3.5.0
42+
* compiler plugin: 3.8.0
43+
* surefire and failsafe: 2.22.0
44+
* plugins using ASM (e.g. the shade plugin) will likely need to be updated as well
45+
46+
```xml
47+
<plugin>
48+
<artifactId>maven-compiler-plugin</artifactId>
49+
<configuration>
50+
<release>13</release>
51+
<compilerArgs>
52+
--enable-preview
53+
</compilerArgs>
54+
</configuration>
55+
</plugin>
56+
<plugin>
57+
<artifactId>maven-surefire-plugin</artifactId>
58+
<configuration>
59+
<argLine>--enable-preview</argLine>
60+
</configuration>
61+
</plugin>
62+
<plugin>
63+
<artifactId>maven-failsafe-plugin</artifactId>
64+
<configuration>
65+
<argLine>--enable-preview</argLine>
66+
</configuration>
67+
</plugin>
68+
```
69+
70+
## Links
71+
72+
* [Java 13 Guide](https://blog.codefx.org/java/java-13-guide/)
73+
* [Switch Expression](https://blog.codefx.org/java/switch-expressions/)
74+
* [Text Block Guid](http://cr.openjdk.java.net/~jlaskey/Strings/TextBlocksGuide_v9.html)

java-13/SwitchExpressionTests.java

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Same example from Java 12 but with syntax update.
3+
*/
4+
public class SwitchExpressionTests {
5+
public static void main(String ... args) {
6+
compatibleWithOldVersion();
7+
8+
longVersion();
9+
shortVersion();
10+
11+
switchWithReturn();
12+
13+
inferringType();
14+
}
15+
16+
public static void compatibleWithOldVersion() {
17+
int luckNumber = 7;
18+
19+
// we can use the old syntax (case:)
20+
var wasLucky = switch (luckNumber) {
21+
// here the fall-through happens (if there is no break or return the next case will be executed)
22+
case 7:
23+
yield false;
24+
case 13:
25+
yield true;
26+
// here we must provide a default case (compiler checks exhaustiveness)
27+
default:
28+
throw new IllegalArgumentException("Wrong number");
29+
}; // switch expression needs ;
30+
31+
System.out.println("\nWas I lucky? " + wasLucky);
32+
}
33+
34+
public static void longVersion() {
35+
var mood = Incomes.Good;
36+
37+
// arrow syntax with no fall-through (only the arrow block will be executed)
38+
var shouldIGo = switch (mood) {
39+
// accepts multiple case labels
40+
case Good, Regular -> {
41+
yield true;
42+
}
43+
case Bad -> {
44+
yield false;
45+
}
46+
// there is no need to use default (we covered all possible cases)
47+
};
48+
49+
System.out.println("\nShould I go? " + shouldIGo);
50+
}
51+
52+
public static void shortVersion() {
53+
var mood = Incomes.Good;
54+
55+
// cool syntax with no fall-through (only the arrow block will be executed)
56+
var shouldIGo = switch (mood) {
57+
// accepts multiple case labels
58+
case Good, Regular -> true;
59+
case Bad -> false;
60+
// there is no need to use default (we covered all possible cases)
61+
};
62+
63+
System.out.println("\nShould I go? " + shouldIGo);
64+
}
65+
66+
public static void switchWithReturn() {
67+
var mood = Incomes.Bad;
68+
69+
// used as statement (there is no assign)
70+
switch (mood) {
71+
case Good -> {
72+
var easy = true;
73+
System.out.println("\nKeep working");
74+
}
75+
case Regular, Bad -> {
76+
// we can now declare vars with same name (each arrow has its own scope)
77+
var easy = true;
78+
System.out.println("\nGo home");
79+
/*
80+
* we can only return inside a block and when switch
81+
* is used as statement
82+
*/
83+
return;
84+
}
85+
}
86+
}
87+
88+
public static void inferringType() {
89+
var mood = Incomes.Good;
90+
91+
// compiler infers the most specific supertype
92+
// of String and IllegalArgumentException = Serializable
93+
// Serializable whichType // we can only use its supertype
94+
var whichType = switch (mood) {
95+
case Good -> "All right";
96+
default -> new IllegalArgumentException("Not right");
97+
};
98+
99+
// won't work
100+
// System.out.println("What is the length? " + whichType.length());
101+
}
102+
}
103+
104+
enum Incomes {
105+
Good,
106+
Regular,
107+
Bad
108+
}

java-13/TextBlocksExample.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public class TextBlocksExample {
2+
public static void main(String[] args) {
3+
// Error (same line)
4+
// String name = """Pat Q. Smith""";
5+
6+
// 3 double-quote must be followed by line terminator (new line)
7+
String niceJson = """
8+
{
9+
"name": "Odair Jose",
10+
"language": "Java"
11+
}"""; // same line to not append \n at the end
12+
System.out.println(niceJson);
13+
14+
// the identation is kept based on the most left char of block (incidental white spaces)
15+
String html = """
16+
<html>
17+
<body>
18+
<p>Hello World.</p>
19+
</body>
20+
</html>
21+
"""; // will append a \n at the end
22+
System.out.println(html);
23+
24+
String badJson = """
25+
{
26+
"name": "Odair Jose",
27+
"language": "Java"
28+
}""";
29+
System.out.println(badJson);
30+
}
31+
}

0 commit comments

Comments
 (0)