Java 15 — What’s new for developers ??

Prateek chaudhary
3 min readDec 22, 2020

Java 15 reached General Availability on 15 September 2020. Let’s see the list of feature changes for developers -

1. Text Blocks

The multi-line (text-blocks) feature is finally supported as a permanent feature in Java 15.

Without text blocks

String text = "<html>\n" +
" <body>\n" +
" <p>Hello Word !!!</p>\n" +
" </body>\n" +
"</html>\n";

With text blocks

String text = """
<html>
<body>
<p>Hello World !!!</p>
</body>
</html>
""";

2. Hidden Classes

Hidden classes are not discoverable and are short-lived. It’s good for developers when classes are generated dynamically at runtime. They are intended to be used by frameworks to generate class at runtime and use indirectly.

The only way for other classes to use a hidden class is indirectly, via its Class object but code in a hidden class can use the hidden class directly, without relying on the Class object.

3. Sealed Classes ( Preview )

Support for sealed classes and interfaces are added as a preview feature. It restricts which other classes or interfaces may extend or implement them.
A sealed class or interface can be extended or implemented only by those classes and interfaces permitted to do so. Also provides a more declarative way than access modifiers to restrict the use of a superclass.

4. Records ( Second Preview )

Records (Records are a new kind of class in the Java language) were added as a preview feature in Java 14. In Java 15 records are again added in previews to support additional forms of local classes and interfaces in the Java language.

Records work well with sealed types (JEP 360). A family of records can implement the same sealed interface

A record class is implicitly final, and cannot be abstract

5. Pattern Matching for instanceof ( Second Preview )

Pattern matching was added as a preview feature in Java 14. In Java 15 this feature is again added as a preview for additional feedback.
With this, we can check the object type, cast it, and bind it in a single line.

Before -

if (obj instanceof String) {
String s = (String) obj;
// use s
}

After -

if (obj instanceof String s) {
// can use s here
} else {
// can't use s here
}

Resources

https://openjdk.java.net/projects/jdk/15/

https://cr.openjdk.java.net/~briangoetz/amber/pattern-match.html

--

--