SlideShare a Scribd company logo
Getting The Most From
Modern Java
Presented by Simon Ritter, Deputy CTO | Azul Systems Inc.
Introduction
2
•Six-month release cadence
•Seven releases since JDK 9
•More features being delivered faster than ever before
Java has changed…
…a lot
Incubator Modules
3
• Defined by JEP 11
• Non-final APIs and non-final tools
‒ Deliver to developers to solicit feedback
‒ Can result in changes or even removal
‒ First example: HTTP/2 API (Introduced in JDK 9, final in JDK 11)
Preview Features
4
• Defined by JEP 12
• New feature of the Java language, JVM or Java SE APIs
‒ Fully specified, fully implemented but not permanent
‒ Solicit developer real-world use and experience
‒ May lead to becoming a permanent feature in future release
• Must be explicitly enabled
‒ javac --release 14 --enable-preview ...
‒ java --enable-preview ...
• Preview APIs
‒ May be required for a preview language feature
‒ Part of the Java SE API (java or javax namespace)
JDK 12
Switch Expressions (Preview)
6
• Switch construct was a statement
‒ No concept of generating a result that could be assigned
• Rather clunky syntax
‒ Every case statement needs to be separated
‒ Must remember break (default is to fall through)
‒ Scope of local variables is not intuitive
Old-Style Switch Statement
7
int numberOfLetters;
switch (day) {
case MONDAY:
case FRIDAY:
case SUNDAY:
numberOfLetters = 6;
break;
case TUESDAY:
numberOfLetters = 7;
break;
case THURSDAY:
case SATURDAY:
numberOfLetters = 8;
break;
case WEDNESDAY:
numberOfLetters = 9;
break;
default:
throw new IllegalStateException("Huh?: " + day); };
New-Style Switch Expression
int numberOfLetters = switch (day) {
case MONDAY, FRIDAY, SUNDAY -> 6;
case TUESDAY -> 7;
case THURSDAY, SATURDAY -> 8;
case WEDNESDAY -> 9;
default -> throw new IllegalStateException("Huh?: " + day);
};
New Old-Style Switch Expression
int numberOfLetters = switch (day) {
case MONDAY:
case FRIDAY:
case SUNDAY:
break 6;
case TUESDAY
break 7;
case THURSDAY
case SATURDAY
break 8;
case WEDNESDAY
break 9;
default:
throw new IllegalStateException("Huh?: " + day);
};
Streams
• New collector, teeing
‒ teeing(Collector, Collector, BiFunction)
• Collect a stream using two collectors
• Use a BiFunction to merge the two collections
10
Collector 1
Collector 2
BiFunction
Stream Result
Streams
11
// Averaging
Double average = Stream.of(1, 4, 5, 2, 1, 7)
.collect(teeing(summingDouble(i -> i), counting(),
(sum, n) -> sum / n));
JDK 13
Text Blocks (Preview)
String webPage = """
<html>
<body>
<p>My web page</p>
</body>
</html>""";
System.out.println(webPage);
$ java WebPage
<html>
<body>
<p>My web page</p>
</body>
</html>
$
incidental white space
Text Blocks (Preview)
String webPage = """
<html>
<body>
<p>My web page</p>
</body>
</html>
""";
System.out.println(webPage);
$ java WebPage
<html>
<body>
<p>My web page</p>
</body>
</html>
$
Additional blank line
incidental white space
Intentional indentation
Switch Expression
int numberOfLetters = switch (day) {
case MONDAY:
case FRIDAY:
case SUNDAY:
break 6;
case TUESDAY
break 7;
case THURSDAY
case SATURDAY
break 8;
case WEDNESDAY
break 9;
default:
throw new IllegalStateException("Huh?: " + day);
};
Switch Expression
int numberOfLetters = switch (day) {
case MONDAY:
case FRIDAY:
case SUNDAY:
yield 6;
case TUESDAY
yield 7;
case THURSDAY
case SATURDAY
yield 8;
case WEDNESDAY
yield 9;
default:
throw new IllegalStateException("Huh?: " + day);
};
JDK 14
Simple Java Data Class
18
class Point {
private final double x;
private final double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double x() {
return x;
}
public double y() {
return y;
}
}
Records (Preview)
19
record Point(double x, double y) { }
record Range(int low, int high) {
public Range { // Compact constructor
if (low > high)
throw new IllegalArgumentException("Bad values");
}
}
Record Additional Details
20
• Compact constructor can only throw unchecked exception
‒ Syntax does not allow for specifying a checked exception
• Object methods equals(), hashCode() and toString() can be
overridden
• The base class of all records is java.lang.Record
‒ This is an example of a preview feature Java SE API
‒ Records cannot sub-class (but may implement interfaces)
• Records do not follow the Java bean pattern
‒ x() not getX() in previous example
• Instance fields cannot be added to a record
‒ Static fields can
• Records can be generic
Using instanceof
21
if (obj instanceof String) {
String s = (String)obj;
System.out.println(s.length());
}
Pattern Matching instanceof (Preview)
22
if (obj instanceof String s)
System.out.println(s.length());
else
// Use of s not allowed here
if (obj instanceof String s && s.length() > 0)
System.out.println(s.length());
// Compiler error
if (obj instanceof String s || s.length() > 0)
System.out.println(s.length());
Pattern Matching instanceof (Preview)
23
if (!(o instanceof String s && s.length() > 3)
return;
System.out.println(s.length());
Text Blocks
• Second preview
• Two new escape sequences
String continuous = """This line will not 
contain a newline in the middle
and solves the extra blank line issue 
""";
String endSpace = """This line will not s
lose the trailing spaces s""";
Helpful NullPointerException
25
• Who's never had an NullPointerException?
• Enabled with -XX:+ShowCodeDetailsInExceptionMessages
a.b.c.i = 99;
Exception in thread "main" java.lang.NullPointerException
at Prog.main(Prog.java:5)
Exception in thread "main" java.lang.NullPointerException:
Cannot read field "c" because "a.b" is null
at Prog.main(Prog.java:5)
JDK 15
Java Inheritance
27
• A class (or interface) in Java can be sub-classed by any class
‒ Unless it is marked as final
Shape
Triangle Square Pentagon
Sealed Classes (JEP 360)
28
• Preview feature
• Sealed classes allow control over which classes can sub-class a class
‒ Think of final as the ultimate sealed class
• Although called sealed classes, this also applies to interfaces
Sealed Classes (JEP 360)
29
• Uses contextual keywords
‒ New idea replacing restricted identifiers and keywords
‒ sealed, permits and non-sealed
• Classes must all be in the same package or module
public sealed class Shape permits Triangle, Square, Pentagon { ... }
Shape
Triangle Square Pentagon Circle
X
Sealed Classes (JEP 360)
30
• All sub-classes must have inheritance capabilities explicitly specified
// Restrict sub-classes to defined set
public sealed class Triangle permits Equilateral, Isosoles extends Shape { ... }
// Prevent any further sub-classing
public final class Square extends Shape { ... }
// Allow any classes to sub-class this one (open)
public non-sealed class Pentagon extends Shape { ... }
Contextual Keyword Humour
31
int non = 2;
int sealed = 1;
var var = non-sealed;
Hidden Classes (JEP 371)
32
• JVM rather than language-level feature
• Classes that cannot be used directly by the bytecodes of other classes
• Several situations where bytecodes generated at runtime
‒ Use of invokedynamic bytecode
‒ Lambdas are a good example
‒ Mostly bound to static class (not for use elsewhere)
‒ Often only used for short time
• Hidden classes can only be accessed via reflection
‒ Primarily intended for framework developers
Records (Second Preview)
33
• Record fields are now (really) final
‒ Cannot be changed via reflection (will throw IllegalAccessException)
• Native methods now explicitly prohibited
‒ Could introduce behaviour dependent on external state
Records (Second Preview)
34
• Local records
‒ Like a local class
‒ Implicitly static
List<Seller> findTopSellers(List<Seller> sellers, int month) {
// Local record
record Sales(Seller seller, double sales) {}
return sellers.stream()
.map(seller -> new Sales(seller, salesInMonth(seller, month)))
.sorted((s1, s2) -> Double.compare(s2.sales(), s1.sales()))
.map(Sales::seller)
.collect(toList());
}
Records (Second Preview)
35
• Records work with sealed classes (interfaces)
public sealed interface Car permits RedCar, BlueCar { ... }
public record RedCar(int w) implements Car { ... }
public record BlueCar(long w, int c) implements Car { ... }
JDK 16
Pattern Matching instanceof (JEP 394)
37
• Now final, i.e. part of the Java SE specification
• Two minor changes to previous iterations
‒ Pattern variables are no longer implicitly final
‒ Compile-time error to compare an expression of type S against a pattern of type T where S
is a sub-type of T
static void printUpperLeftColoredPoint(Rectangle r) {
if (r instanceof Rectangle rect) {
System.out.println(rect);
}
}
| Error:
| pattern type Rectangle is a subtype of expression type Rectangle
| if (r instanceof Rectangle rect) {
| ^-------------------------^
Add UNIX-Domain Socket Channels
38
• Add UNIX_AF socket channels
‒ Used for IPC on UNIX-based OSs and Windows
• Better security and performance than TCP/IP loopback connections
‒ Behaviour is identical
• No constructor, use factory methods
var unix = UnixDomainSocketAddress.of("/tmp/foo");
Streams mapMulti
39
• Similar to flatMap
‒ Each element on the input stream is mapped to zero or more elements on the output
stream
‒ Difference is that a mapping can be applied at the same time
‒ Uses a BiConsumer
Stream.of("Java", "Python", "JavaScript", "C#", "Ruby", "")
.mapMulti((str, consumer) -> {
for (int i = 0; i < str.length(); i++)
consumer.accept(str.length());
})
.forEach(i -> System.out.print(i + " "));
// 4 4 4 4 6 6 6 6 6 6 10 10 10 10 10 10 10 10 10 10 2 2 4 4 4 4
Vector API (JEP 338)
40
• Incubator module (not part of the Java SE specification)
• API to express vector computations
‒ Compile at runtime to optimal hardware instructions
‒ Deliver superior performance to equivalent scalar operations
• Ideally, this would not be necessary
‒ Compiler should identify where vector operations can be used
Vector API (JEP 338)
41
void scalarComputation(float[] a, float[] b, float[] c) {
for (int i = 0; i < a.length; i++)
c[i] = (a[i] * a[i] + b[i] * b[i]) * -1.0f;
}
static final VectorSpecies<Float> SPECIES = FloatVector.SPECIES_256;
void vectorComputation(float[] a, float[] b, float[] c) {
for (int i = 0; i < a.length; i += SPECIES.length()) {
var m = SPECIES.indexInRange(i, a.length);
var va = FloatVector.fromArray(SPECIES, a, i, m);
var vb = FloatVector.fromArray(SPECIES, b, i, m);
var vc = va.mul(va).
add(vb.mul(vb)).
neg();
vc.intoArray(c, i, m);
}
}
Foreign-Memory Access API (JEP 393)
42
• Introduced in JDK 14, now third incubator iteration
• API for safe and efficient access to memory outside of the Java heap
• MemorySegment
‒ Models a contiguous area of memory
• MemoryAddress
‒ Models an individual memory address (on or off heap)
• MemoryLayout
‒ Programmatic description of a MemorySegment
try (MemorySegment segment = MemorySegment.allocateNative(100)) {
for (int i = 0; i < 25; i++)
MemoryAccess.setIntAtOffset(segment, i * 4, i);
}
Foreign-Memory Access API (JEP 393)
43
• Example using MemoryLayout and VarHandle
‒ Simpler access of structured data
SequenceLayout intArrayLayout
= MemoryLayout.ofSequence(25,
MemoryLayout.ofValueBits(32,
ByteOrder.nativeOrder()));
VarHandle indexedElementHandle
= intArrayLayout.varHandle(int.class,
PathElement.sequenceElement());
try (MemorySegment segment = MemorySegment.allocateNative(intArrayLayout)) {
for (int i = 0; i < intArrayLayout.elementCount().getAsLong(); i++)
indexedElementHandle.set(segment, (long) i, i);
}
Foreign Linker API (JEP 389): Incubator
44
• Provides statically-typed, pure-Java access to native code
‒ Works in conjunction with the Foreign Memory Access API
‒ Initially targeted at C native code. C++ should follow
• More powerful when combined with Project Panama jextract command
public static void main(String[] args) throws Throwable {
var linker = CLinker.getInstance();
var lookup = LibraryLookup.ofDefault();
// get a native method handle for 'getpid' function
var getpid = linker.downcallHandle(lookup.lookup("getpid").get(),
MethodType.methodType(int.class),
FunctionDescriptor.of(CLinker.C_INT));
System.out.println((int)getpid.invokeExact());
}
Warnings for Value-Based Classes
45
• Part of Project Valhalla, which adds value-types to Java
‒ Introduces the concept of primitive classes
• Primitive wrapper classes (Integer, Float, etc.) designated value-based
‒ Constructors were deprecated in JDK 9
‒ Now marked as for removal
‒ Attempting to synchronize on an instance of a value-based class will issue a warning
Strongly Encapsulate JDK Internals By Default
46
• Encapsulation of JDK internal APIs started in JDK 9
‒ Part of modularity (Project Jigsaw)
• Side effect was the potential to break many applications and frameworks
‒ Spring, etc.
• The big kill switch was included to get round this
‒ --illegal-access
‒ Four options: permit, warn, debug, deny
‒ Until now default has been permit
‒ New default is deny
• Critical internal APIs are still not encapsulated
‒ Including sun.misc.Unsafe
Summary
Zulu Enterprise
48
• Enhanced build of OpenJDK source code
 Fully TCK tested
 JDK 6, 7, 8, 11 and 13
 TLS1.3, Flight Recorder backports
• Wide platform support:
 Intel 64-bit Windows, Mac, Linux
 Intel 32-bit Windows and Linux
• Real drop-in replacement for Oracle JDK
 Many enterprise customers
 No reports of any compatibility issues
Conclusions
49
• The six-month release cycle is working well
• JDK 12 to JDK 16 contains lots of great new features
• Start preparing for JDK 17, the next LTS
‒ Test with JDK 16
• Use Zulu builds of OpenJDK if you want to deploy to production
Questions?

More Related Content

Similar to Modern_Java_Workshop manjunath np hj slave (20)

PPTX
What is new in Java 8
Sandeep Kr. Singh
 
PDF
Java 17
Mutlu Okuducu
 
PPT
Core java by a introduction sandesh sharma
Sandesh Sharma
 
PDF
Java 22 and Beyond- A Roadmap of Innovations
Ana-Maria Mihalceanu
 
PPTX
The Art of Java Type Patterns
Simon Ritter
 
PPTX
Keeping Up with Java: Look at All These New Features!
VMware Tanzu
 
PDF
Amber and beyond: Java language changes
Stephen Colebourne
 
PPTX
A brief tour of modern Java
Sina Madani
 
PPTX
Java8: what's new and what's hot
Sergii Maliarov
 
PPT
Java user group 2015 02-09-java8
Marc Tritschler
 
PPT
Java user group 2015 02-09-java8
marctritschler
 
PDF
Highlights from Java 10, 11 and 12 and Future of Java at JUG Koblenz
Vadym Kazulkin
 
PPTX
Java after 8
Simon Ritter
 
DOCX
Jist of Java
Nikunj Parekh
 
DOCX
JAVA CONCEPTS AND PRACTICES
Nikunj Parekh
 
PDF
What did you miss in Java from 9-13?
relix1988
 
PDF
Java Full Throttle
José Paumard
 
PPTX
Java9to19Final.pptx
iFour Technolab Pvt. Ltd.
 
PDF
Java concepts and questions
Farag Zakaria
 
DOC
MX Server is my friend
Gabriel Daty
 
What is new in Java 8
Sandeep Kr. Singh
 
Java 17
Mutlu Okuducu
 
Core java by a introduction sandesh sharma
Sandesh Sharma
 
Java 22 and Beyond- A Roadmap of Innovations
Ana-Maria Mihalceanu
 
The Art of Java Type Patterns
Simon Ritter
 
Keeping Up with Java: Look at All These New Features!
VMware Tanzu
 
Amber and beyond: Java language changes
Stephen Colebourne
 
A brief tour of modern Java
Sina Madani
 
Java8: what's new and what's hot
Sergii Maliarov
 
Java user group 2015 02-09-java8
Marc Tritschler
 
Java user group 2015 02-09-java8
marctritschler
 
Highlights from Java 10, 11 and 12 and Future of Java at JUG Koblenz
Vadym Kazulkin
 
Java after 8
Simon Ritter
 
Jist of Java
Nikunj Parekh
 
JAVA CONCEPTS AND PRACTICES
Nikunj Parekh
 
What did you miss in Java from 9-13?
relix1988
 
Java Full Throttle
José Paumard
 
Java9to19Final.pptx
iFour Technolab Pvt. Ltd.
 
Java concepts and questions
Farag Zakaria
 
MX Server is my friend
Gabriel Daty
 

Recently uploaded (20)

PDF
hr generalist training.pdf..............
a25075044
 
PDF
Crafting Winning CVs Cover Letters and Mastering Job Interviews with Roman Vi...
Excellence Foundation for South Sudan
 
PPTX
WAT - 2 - Copy.pptxAT - 1.pptx review this to get sutaible help
pranksta001
 
PPT
CLEFT LIP AND PALATE CLP for the students.ppt
ssuser2865ca
 
PPTX
GSM GPRS & CDMA.pptxbhhhhhhhhjhhhjjjjjjjj
saianuragk33
 
PDF
hr generalist certification.pdf.........
a25075044
 
PPT
SQL.pptkarim pfe rabatkarim pfe rabatkarim pfe rabat
Keeyvikyv
 
PDF
OM Logistics & Warehouse Executive Program
The Learn Skills
 
PDF
Ch7.pdf fghjkloiuytrezgdrsrddfhhvhjgufygfgjhfugyfufutfgyufuygfuygfuytfuytftfy...
SuKosh1
 
PDF
Get Daily Sarkari Job Updates – Majhi Naukri
Reeshna Prajeesh
 
PPTX
Larynx_Cancer_Presenvdhhdhhdtaon (1).pptx
esterritesh
 
PDF
Smarter Private Job Search Starts with Formwalaa
Reeshna Prajeesh
 
PDF
Bilal Ibrar | Digital Marketing Expert | Resume | CV
Bilal Ibrar
 
PPT
26 PropheticMovement-notesdsdsdsdsdd.ppt
lawrence762329
 
PDF
hr generalist course in pune.pdf........
a25075044
 
PPTX
Learn AI in Software Testing - Venkatesh (Rahul Shetty)
Venkatesh (Rahul Shetty)
 
PDF
NotificationForTheTeachingPositionsAdvt012025.pdf
sunitsaathi
 
PDF
Sarkari Job Alerts in Marathi & English – Majhi Naukri
Reeshna Prajeesh
 
PPTX
文凭复刻澳洲电子毕业证阳光海岸大学成绩单USC录取通知书
Taqyea
 
PDF
Mastercard Foundation post.pdf documentation
odameamesika
 
hr generalist training.pdf..............
a25075044
 
Crafting Winning CVs Cover Letters and Mastering Job Interviews with Roman Vi...
Excellence Foundation for South Sudan
 
WAT - 2 - Copy.pptxAT - 1.pptx review this to get sutaible help
pranksta001
 
CLEFT LIP AND PALATE CLP for the students.ppt
ssuser2865ca
 
GSM GPRS & CDMA.pptxbhhhhhhhhjhhhjjjjjjjj
saianuragk33
 
hr generalist certification.pdf.........
a25075044
 
SQL.pptkarim pfe rabatkarim pfe rabatkarim pfe rabat
Keeyvikyv
 
OM Logistics & Warehouse Executive Program
The Learn Skills
 
Ch7.pdf fghjkloiuytrezgdrsrddfhhvhjgufygfgjhfugyfufutfgyufuygfuygfuytfuytftfy...
SuKosh1
 
Get Daily Sarkari Job Updates – Majhi Naukri
Reeshna Prajeesh
 
Larynx_Cancer_Presenvdhhdhhdtaon (1).pptx
esterritesh
 
Smarter Private Job Search Starts with Formwalaa
Reeshna Prajeesh
 
Bilal Ibrar | Digital Marketing Expert | Resume | CV
Bilal Ibrar
 
26 PropheticMovement-notesdsdsdsdsdd.ppt
lawrence762329
 
hr generalist course in pune.pdf........
a25075044
 
Learn AI in Software Testing - Venkatesh (Rahul Shetty)
Venkatesh (Rahul Shetty)
 
NotificationForTheTeachingPositionsAdvt012025.pdf
sunitsaathi
 
Sarkari Job Alerts in Marathi & English – Majhi Naukri
Reeshna Prajeesh
 
文凭复刻澳洲电子毕业证阳光海岸大学成绩单USC录取通知书
Taqyea
 
Mastercard Foundation post.pdf documentation
odameamesika
 
Ad

Modern_Java_Workshop manjunath np hj slave

  • 1. Getting The Most From Modern Java Presented by Simon Ritter, Deputy CTO | Azul Systems Inc.
  • 2. Introduction 2 •Six-month release cadence •Seven releases since JDK 9 •More features being delivered faster than ever before Java has changed… …a lot
  • 3. Incubator Modules 3 • Defined by JEP 11 • Non-final APIs and non-final tools ‒ Deliver to developers to solicit feedback ‒ Can result in changes or even removal ‒ First example: HTTP/2 API (Introduced in JDK 9, final in JDK 11)
  • 4. Preview Features 4 • Defined by JEP 12 • New feature of the Java language, JVM or Java SE APIs ‒ Fully specified, fully implemented but not permanent ‒ Solicit developer real-world use and experience ‒ May lead to becoming a permanent feature in future release • Must be explicitly enabled ‒ javac --release 14 --enable-preview ... ‒ java --enable-preview ... • Preview APIs ‒ May be required for a preview language feature ‒ Part of the Java SE API (java or javax namespace)
  • 6. Switch Expressions (Preview) 6 • Switch construct was a statement ‒ No concept of generating a result that could be assigned • Rather clunky syntax ‒ Every case statement needs to be separated ‒ Must remember break (default is to fall through) ‒ Scope of local variables is not intuitive
  • 7. Old-Style Switch Statement 7 int numberOfLetters; switch (day) { case MONDAY: case FRIDAY: case SUNDAY: numberOfLetters = 6; break; case TUESDAY: numberOfLetters = 7; break; case THURSDAY: case SATURDAY: numberOfLetters = 8; break; case WEDNESDAY: numberOfLetters = 9; break; default: throw new IllegalStateException("Huh?: " + day); };
  • 8. New-Style Switch Expression int numberOfLetters = switch (day) { case MONDAY, FRIDAY, SUNDAY -> 6; case TUESDAY -> 7; case THURSDAY, SATURDAY -> 8; case WEDNESDAY -> 9; default -> throw new IllegalStateException("Huh?: " + day); };
  • 9. New Old-Style Switch Expression int numberOfLetters = switch (day) { case MONDAY: case FRIDAY: case SUNDAY: break 6; case TUESDAY break 7; case THURSDAY case SATURDAY break 8; case WEDNESDAY break 9; default: throw new IllegalStateException("Huh?: " + day); };
  • 10. Streams • New collector, teeing ‒ teeing(Collector, Collector, BiFunction) • Collect a stream using two collectors • Use a BiFunction to merge the two collections 10 Collector 1 Collector 2 BiFunction Stream Result
  • 11. Streams 11 // Averaging Double average = Stream.of(1, 4, 5, 2, 1, 7) .collect(teeing(summingDouble(i -> i), counting(), (sum, n) -> sum / n));
  • 13. Text Blocks (Preview) String webPage = """ <html> <body> <p>My web page</p> </body> </html>"""; System.out.println(webPage); $ java WebPage <html> <body> <p>My web page</p> </body> </html> $ incidental white space
  • 14. Text Blocks (Preview) String webPage = """ <html> <body> <p>My web page</p> </body> </html> """; System.out.println(webPage); $ java WebPage <html> <body> <p>My web page</p> </body> </html> $ Additional blank line incidental white space Intentional indentation
  • 15. Switch Expression int numberOfLetters = switch (day) { case MONDAY: case FRIDAY: case SUNDAY: break 6; case TUESDAY break 7; case THURSDAY case SATURDAY break 8; case WEDNESDAY break 9; default: throw new IllegalStateException("Huh?: " + day); };
  • 16. Switch Expression int numberOfLetters = switch (day) { case MONDAY: case FRIDAY: case SUNDAY: yield 6; case TUESDAY yield 7; case THURSDAY case SATURDAY yield 8; case WEDNESDAY yield 9; default: throw new IllegalStateException("Huh?: " + day); };
  • 18. Simple Java Data Class 18 class Point { private final double x; private final double y; public Point(double x, double y) { this.x = x; this.y = y; } public double x() { return x; } public double y() { return y; } }
  • 19. Records (Preview) 19 record Point(double x, double y) { } record Range(int low, int high) { public Range { // Compact constructor if (low > high) throw new IllegalArgumentException("Bad values"); } }
  • 20. Record Additional Details 20 • Compact constructor can only throw unchecked exception ‒ Syntax does not allow for specifying a checked exception • Object methods equals(), hashCode() and toString() can be overridden • The base class of all records is java.lang.Record ‒ This is an example of a preview feature Java SE API ‒ Records cannot sub-class (but may implement interfaces) • Records do not follow the Java bean pattern ‒ x() not getX() in previous example • Instance fields cannot be added to a record ‒ Static fields can • Records can be generic
  • 21. Using instanceof 21 if (obj instanceof String) { String s = (String)obj; System.out.println(s.length()); }
  • 22. Pattern Matching instanceof (Preview) 22 if (obj instanceof String s) System.out.println(s.length()); else // Use of s not allowed here if (obj instanceof String s && s.length() > 0) System.out.println(s.length()); // Compiler error if (obj instanceof String s || s.length() > 0) System.out.println(s.length());
  • 23. Pattern Matching instanceof (Preview) 23 if (!(o instanceof String s && s.length() > 3) return; System.out.println(s.length());
  • 24. Text Blocks • Second preview • Two new escape sequences String continuous = """This line will not contain a newline in the middle and solves the extra blank line issue """; String endSpace = """This line will not s lose the trailing spaces s""";
  • 25. Helpful NullPointerException 25 • Who's never had an NullPointerException? • Enabled with -XX:+ShowCodeDetailsInExceptionMessages a.b.c.i = 99; Exception in thread "main" java.lang.NullPointerException at Prog.main(Prog.java:5) Exception in thread "main" java.lang.NullPointerException: Cannot read field "c" because "a.b" is null at Prog.main(Prog.java:5)
  • 27. Java Inheritance 27 • A class (or interface) in Java can be sub-classed by any class ‒ Unless it is marked as final Shape Triangle Square Pentagon
  • 28. Sealed Classes (JEP 360) 28 • Preview feature • Sealed classes allow control over which classes can sub-class a class ‒ Think of final as the ultimate sealed class • Although called sealed classes, this also applies to interfaces
  • 29. Sealed Classes (JEP 360) 29 • Uses contextual keywords ‒ New idea replacing restricted identifiers and keywords ‒ sealed, permits and non-sealed • Classes must all be in the same package or module public sealed class Shape permits Triangle, Square, Pentagon { ... } Shape Triangle Square Pentagon Circle X
  • 30. Sealed Classes (JEP 360) 30 • All sub-classes must have inheritance capabilities explicitly specified // Restrict sub-classes to defined set public sealed class Triangle permits Equilateral, Isosoles extends Shape { ... } // Prevent any further sub-classing public final class Square extends Shape { ... } // Allow any classes to sub-class this one (open) public non-sealed class Pentagon extends Shape { ... }
  • 31. Contextual Keyword Humour 31 int non = 2; int sealed = 1; var var = non-sealed;
  • 32. Hidden Classes (JEP 371) 32 • JVM rather than language-level feature • Classes that cannot be used directly by the bytecodes of other classes • Several situations where bytecodes generated at runtime ‒ Use of invokedynamic bytecode ‒ Lambdas are a good example ‒ Mostly bound to static class (not for use elsewhere) ‒ Often only used for short time • Hidden classes can only be accessed via reflection ‒ Primarily intended for framework developers
  • 33. Records (Second Preview) 33 • Record fields are now (really) final ‒ Cannot be changed via reflection (will throw IllegalAccessException) • Native methods now explicitly prohibited ‒ Could introduce behaviour dependent on external state
  • 34. Records (Second Preview) 34 • Local records ‒ Like a local class ‒ Implicitly static List<Seller> findTopSellers(List<Seller> sellers, int month) { // Local record record Sales(Seller seller, double sales) {} return sellers.stream() .map(seller -> new Sales(seller, salesInMonth(seller, month))) .sorted((s1, s2) -> Double.compare(s2.sales(), s1.sales())) .map(Sales::seller) .collect(toList()); }
  • 35. Records (Second Preview) 35 • Records work with sealed classes (interfaces) public sealed interface Car permits RedCar, BlueCar { ... } public record RedCar(int w) implements Car { ... } public record BlueCar(long w, int c) implements Car { ... }
  • 37. Pattern Matching instanceof (JEP 394) 37 • Now final, i.e. part of the Java SE specification • Two minor changes to previous iterations ‒ Pattern variables are no longer implicitly final ‒ Compile-time error to compare an expression of type S against a pattern of type T where S is a sub-type of T static void printUpperLeftColoredPoint(Rectangle r) { if (r instanceof Rectangle rect) { System.out.println(rect); } } | Error: | pattern type Rectangle is a subtype of expression type Rectangle | if (r instanceof Rectangle rect) { | ^-------------------------^
  • 38. Add UNIX-Domain Socket Channels 38 • Add UNIX_AF socket channels ‒ Used for IPC on UNIX-based OSs and Windows • Better security and performance than TCP/IP loopback connections ‒ Behaviour is identical • No constructor, use factory methods var unix = UnixDomainSocketAddress.of("/tmp/foo");
  • 39. Streams mapMulti 39 • Similar to flatMap ‒ Each element on the input stream is mapped to zero or more elements on the output stream ‒ Difference is that a mapping can be applied at the same time ‒ Uses a BiConsumer Stream.of("Java", "Python", "JavaScript", "C#", "Ruby", "") .mapMulti((str, consumer) -> { for (int i = 0; i < str.length(); i++) consumer.accept(str.length()); }) .forEach(i -> System.out.print(i + " ")); // 4 4 4 4 6 6 6 6 6 6 10 10 10 10 10 10 10 10 10 10 2 2 4 4 4 4
  • 40. Vector API (JEP 338) 40 • Incubator module (not part of the Java SE specification) • API to express vector computations ‒ Compile at runtime to optimal hardware instructions ‒ Deliver superior performance to equivalent scalar operations • Ideally, this would not be necessary ‒ Compiler should identify where vector operations can be used
  • 41. Vector API (JEP 338) 41 void scalarComputation(float[] a, float[] b, float[] c) { for (int i = 0; i < a.length; i++) c[i] = (a[i] * a[i] + b[i] * b[i]) * -1.0f; } static final VectorSpecies<Float> SPECIES = FloatVector.SPECIES_256; void vectorComputation(float[] a, float[] b, float[] c) { for (int i = 0; i < a.length; i += SPECIES.length()) { var m = SPECIES.indexInRange(i, a.length); var va = FloatVector.fromArray(SPECIES, a, i, m); var vb = FloatVector.fromArray(SPECIES, b, i, m); var vc = va.mul(va). add(vb.mul(vb)). neg(); vc.intoArray(c, i, m); } }
  • 42. Foreign-Memory Access API (JEP 393) 42 • Introduced in JDK 14, now third incubator iteration • API for safe and efficient access to memory outside of the Java heap • MemorySegment ‒ Models a contiguous area of memory • MemoryAddress ‒ Models an individual memory address (on or off heap) • MemoryLayout ‒ Programmatic description of a MemorySegment try (MemorySegment segment = MemorySegment.allocateNative(100)) { for (int i = 0; i < 25; i++) MemoryAccess.setIntAtOffset(segment, i * 4, i); }
  • 43. Foreign-Memory Access API (JEP 393) 43 • Example using MemoryLayout and VarHandle ‒ Simpler access of structured data SequenceLayout intArrayLayout = MemoryLayout.ofSequence(25, MemoryLayout.ofValueBits(32, ByteOrder.nativeOrder())); VarHandle indexedElementHandle = intArrayLayout.varHandle(int.class, PathElement.sequenceElement()); try (MemorySegment segment = MemorySegment.allocateNative(intArrayLayout)) { for (int i = 0; i < intArrayLayout.elementCount().getAsLong(); i++) indexedElementHandle.set(segment, (long) i, i); }
  • 44. Foreign Linker API (JEP 389): Incubator 44 • Provides statically-typed, pure-Java access to native code ‒ Works in conjunction with the Foreign Memory Access API ‒ Initially targeted at C native code. C++ should follow • More powerful when combined with Project Panama jextract command public static void main(String[] args) throws Throwable { var linker = CLinker.getInstance(); var lookup = LibraryLookup.ofDefault(); // get a native method handle for 'getpid' function var getpid = linker.downcallHandle(lookup.lookup("getpid").get(), MethodType.methodType(int.class), FunctionDescriptor.of(CLinker.C_INT)); System.out.println((int)getpid.invokeExact()); }
  • 45. Warnings for Value-Based Classes 45 • Part of Project Valhalla, which adds value-types to Java ‒ Introduces the concept of primitive classes • Primitive wrapper classes (Integer, Float, etc.) designated value-based ‒ Constructors were deprecated in JDK 9 ‒ Now marked as for removal ‒ Attempting to synchronize on an instance of a value-based class will issue a warning
  • 46. Strongly Encapsulate JDK Internals By Default 46 • Encapsulation of JDK internal APIs started in JDK 9 ‒ Part of modularity (Project Jigsaw) • Side effect was the potential to break many applications and frameworks ‒ Spring, etc. • The big kill switch was included to get round this ‒ --illegal-access ‒ Four options: permit, warn, debug, deny ‒ Until now default has been permit ‒ New default is deny • Critical internal APIs are still not encapsulated ‒ Including sun.misc.Unsafe
  • 48. Zulu Enterprise 48 • Enhanced build of OpenJDK source code  Fully TCK tested  JDK 6, 7, 8, 11 and 13  TLS1.3, Flight Recorder backports • Wide platform support:  Intel 64-bit Windows, Mac, Linux  Intel 32-bit Windows and Linux • Real drop-in replacement for Oracle JDK  Many enterprise customers  No reports of any compatibility issues
  • 49. Conclusions 49 • The six-month release cycle is working well • JDK 12 to JDK 16 contains lots of great new features • Start preparing for JDK 17, the next LTS ‒ Test with JDK 16 • Use Zulu builds of OpenJDK if you want to deploy to production