What are the new features of java-15?
On September 2020, Oracle released a new version of Java as Java-15. This is a short-term release for the JDK platform. Following are the new features of java-15.
Records(Preview Feature):
- This feature was originally introduced by java-14 as a preview feature.
- This is added to enhance the Java programming language with records, which are classes that act as transparent carriers for immutable data. Records can be thought of as nominal tuples. This is a preview language feature in JDK 15.
- Let’s see examples with and without Records.
- Without the record, we would create an immutable data transfer object (DTO) as shown in the below example.
Output:
- With Records
Output:
- As we can see from the above 2 examples, without the record feature we need to write too much code for making a immutable class, and to print the class object we need to override the toString method explicitly.
- As we can see from the second example the class definition has a new syntax specific for records. In the header, we provide the details about the fields inside the record.
- Using this header, the compiler can infer the internal fields. Hence we don’t have to define specific member variables and accessors, as they’re provided by default. We also don’t have to provide a constructor.
- Also, the compiler provides sensible implementations for the toString, equals, and hashCode methods.
Hidden Classes
- Hidden classes, which are classes that cannot be used directly by the bytecode of other classes.
- Hidden classes are intended for use by frameworks that generate classes at run time and use them indirectly, via reflection.
- A hidden class may be defined as a member of an access control nest and may be unloaded independently of other classes.
- While most developers won’t find a direct benefit from them, anyone who works with dynamic bytecode or JVM languages will likely find them useful.
Sealed Classes(Preview Feature)
- Java-15 introduced the concept of sealed classes. It is a preview feature. Java-sealed classes and interfaces restrict which classes and interfaces may extend or implement them.
- we can say that the class that cannot be inherited but can be instantiated is known as the sealed class.
- It allows classes and interfaces to have more control over their permitted subtypes.
- Note that the concept of sealed classes is a preview feature, not a permanent feature.
Example:
Pattern Matching Type Checks(Preview):
- This preview feature is introduced in java-14 and the same is continued in java-15 with no new enhancements.
- This feature is to remove a lot of boilerplate code that typically comes with the instanceof operator:
- The pattern-matching feature simplifies this by introducing a new binding variable:
- Old code:
- New code after pattern-matching is introduced.
Foreign Memory API(Preview):
- Foreign-Memory Access API is also a preview feature introduced in java-14 and java-15 continues it as a preview feature with some new features.
- A new VarHandle API, to customize memory access var handles
- Support for parallel processing of a memory segment using the Spliterator interface
- Enhanced support for mapped memory segments
- Ability to manipulate and dereference addresses coming from things like native calls
Garbage Collector
ZGC: A Scalable Low-Latency Garbage Collector and Shenandoah: A Low-Pause-Time Garbage Collector
Both are supported configurations that teams can opt to use, while the G1 collector will remain the default.
Both were previously available as a preview feature. This approach allows developers to test the new garbage collectors and submit feedback without downloading a separate JDK or add-on.
Note that Shenandoah: it isn’t available from all vendor JDKs — most notably, Oracle JDK doesn’t include it.
Also, see:
- New features of Java-8
- New features of Java-9
- New features of Java-10
- New features of Java-11
- New features of Java-12
- New features of Java-13
- New features of Java-14