Java 15 released! What is new?

  • Post last modified:December 15, 2022
  • Reading time:5 mins read

By following its 6 — month release cycle, Java 15 becomes the 2nd release of 2020. Every 3-year LTS version will be released, and the next LTS will be Java 17 in 2021. And you can see that release was consistent with the planned schedule.

New Features

1: Text Blocks

  • Text block is a multi-line string literal that avoids the need for most escape sequences, automatically formats the string in a predictable way, and gives the developer control over format when desired
  • Started as a preview feature in Java 13 & 14. Now Official in Java 15.
  • Without Text Block, we need append newline character and escape “ .
// Without Text Block
String jsnWithoutTxtBlck = "{\n"+
                           "  \"a\": "+ "\"b\""+"\n"+
                           "}";
System.out.println(jsnWithoutTxtBlck);
  • Text Block code becomes more readable. Trimming whitespaces, Normalizing line endings, and translating escape sequences all done by Java.
//Text Block Example
String jsn = """
                {
                  "a":"b"
                }
             """;
System.out.println(jsn);

//Text Block Example With Formatted
String jsnFormatted = """
                {
                  "a":"%s"
                }
             """.formatted("b");
System.out.println(jsnFormatted);

2: Helpful Null Pointer Exception Message

  • Helpful NullPointer Exception was added in Java 14 in order to improve the NPE message to help developers to debug where Null Pointer occurred. In Java 14 we could add a flag to helpful Null Pointer Exception. https://openjdk.java.net/jeps/358
 — XX:ShowCodeDetailsInExceptionMessages.
  • In Java 15 we don’t need to add this flag anymore.

Preview Features

1: Sealed Classes

  • Sealed types are classes and interfaces which allow only certain subclasses to extend or implement them.
  • But if another subclass which is not in the permit list will try to extend it, then the compiler will throw an error as shown in the picture.
  • Sealed Classes make much sense when we use it with pattern matching and Switch Case. This would make the compiler to raise an error if the case doesn’t match the permit type.
int getCenter(Shape shape) {
    return switch (shape) {
        case Circle c    -> ... c.center() ...
        case Rectangle r -> ... r.length() ...
        case Square s    -> ... s.side() ...
    };
}

2: Pattern Matching (It was also in preview for java 14)

  • Pattern Matching makes code more concise and less verbose. We don’t need to repeatedly write type tests and typecasting for each case. With Pattern matching, we can do tests and cast in one line.

  • Variables dTx and cTx are called pattern variables and only available in the scope of the block it been used. For the cTx case, this variable cannot be used outside of else if block as it is not a normal variable.
  • For more information visit Java Enhancement Proposal (Jep): https://openjdk.java.net/jeps/305

3: Records (It was also in preview for java 14)

  • Records are data class that sole purpose is to use as data carrier ( data transfer object aks DTO’s).
  • In Java often when we create DTOs we need to create a constructor, setters/getters, equals & hashcode. These tasks are pretty repetitive and often consider boilerplate code. To make Java less verbose introduction to record in java is an awaited step.
  • Now developers can clear their intention of making a class as a data carrier by writing a record keyword before the class name, which makes the object an immutable data object. Look at the example below:

  • Now once the developer makes the class as a record, all the methods like getter and equals(), hashcode() available out of the box. This surely better design saves developers time and removes some level of verbosity from Java.
  • For more information visit Java Enhancement Proposal (Jep):https://openjdk.java.net/jeps/359

JVM Improvements (Production Ready)

1: Garbage Collectors

  • ZGC become a product feature and exited experimental status. (Jep:https://openjdk.java.net/jeps/377)
  • ZGC is a low latency garbage collector that has pause time under 10 ms & can scale up to terabyte heaps. Provides great performance without tunning.
  • Shenandoah GC also become a product feature and exited experimental status. (Jep: https://openjdk.java.net/jeps/379)
  • Shenandoah GC developed by the Redhat team and also compete with ZGC in low latency and concurrent garbage collector.
-XX:+UseZGC // Flag to use ZGC-XX:+UseShenandoahGC //Flag to use Shenandoah GC

2: Hidden Classes finds its way into java 15, its useful for frameworks that emit bytecode dynamically and desire to use various properties such as Non-discoverability, Access control, Lifecycle .(Jep:https://openjdk.java.net/jeps/371)

3: Edward Curve Digital Signature Algorithm has been added to Java 15. (Jep: https://openjdk.java.net/jeps/339)

4: Legacy DatagramSocket API is reimplemented and added to Java 15(Jep: https://openjdk.java.net/jeps/373)

Deprecations and Removals

As a Main Highlights

  • The Text block feature is added.
  • Sealed classes added as a preview to controlling inheritance hierarchy.
  • New garbage collectors (ZGC & Shenandoah) were added and they are production-ready.

Bonus Tip

Leave a Reply