Text Block, Sealed Classes, ZGC & Shenandoah Garbage Collector, and many more!

Table of Contents

  • New Features
  • Preview Features
  • JVM Improvements
  • Deprecations and Removals

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.

Image for post

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.

Image for post

  • 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.

Image for post

  • 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() ...
    };
}

#java #programming #developer

What is New in Java 15?
2.80 GEEK