Getty Images

Tip

5 best Java 17 features that developers will love

The new Java 17 release contains plenty of new features that will intrigue developers. Here are five features that should push developers off the Java 11 LTS to JDK 17.

Programmers typically target a specific, long-term support release when they develop applications. That means for the past several years, most Java developers were limited to the Java 8 or Java 11 feature set.

However, a new LTS Java release is now available. There are several improvements in these Java 17 features that will make enterprise software development much easier.

Top 5 Java 17 features

Here are the top five Java 17 features that developers will love when they move on from an older version of the Java platform:

  1. the new Java Record data type;
  2. Java text blocks;
  3. helpful NullPointerException guidance;
  4. switch statement expressions; and
  5. sealed classes.

Java Records

One knock about the Java programming language is its needless verbosity and ceremony, especially compared to other programming languages such as Python or Groovy. The new Java Record data type addresses that complaint.

Quite often, a Java class simply represents data. In a data transfer class, no methods are required, the class has no need to support future extensibility and the data itself is immutable for the life of the object. The new Java Record type embraces these ideals, and its implementation uses a precise and succinct syntax.

record Point(long x, long y) { }

A Record is implicitly final, its state is immutable, it can't be extended and it can't contain instance initializers. The Java Record creates a more readable and understandable syntax, while at the same time, immutable state will allow the JVM to implement efficiencies that will allow programs to perform significant list processing faster, making Java applications more linearly scalable. The Java Record data type is an exciting new addition to the Java language.

Java text blocks

Java has never prided itself on its ability to perform String manipulation.

Previously, the simple task of hard-coding formatted text within a Java program was difficult because it required String terminators, concatenators and esoteric escape sequences that made text embedded within a Java program difficult to both read and write. But in Java 17, you can now use text blocks, which make the use of text in a Java program much more natural.

Compare a traditional block of text embedded within a Java program to the new, Java 17 text block feature.

Prior to Java 17, the text block looked like this:

String oldTextBlock = "<html>\n" +
              "    <body>\n" +
              "        <em>Cool LTS Java 17 Features</em>\n" +
              "    </body>\n" +
              "</html>\n";

Now, it looks like this:

String newTextBlock = """
              <html>
                  <body>
                      <em>Cool LTS Java 17 Features</em>\
                  </body>
              </html>
              """;

This Java 17 feature represents a marked usability improvement from prior iterations.

Helpful NullPointerException guidance

Troubleshooting Java applications typically boils down to a tedious system log scan and a time-consuming analysis of the runtime exceptions that lay within.

Java features
Java features that developers like

A reference to a null object is a common trigger that causes an otherwise well-written program to fail. Unfortunately, the Java logs rarely provide a lot of detail about what the offending object was, or which property the program referenced when it triggered the NullPointException.

In the Java 17 LTS release, developers can take advantage of the new NullPointerException guidance feature. This Java 17 feature improves the usability of an exception's stack trace by logging the exact name of the variable that was null.

Switch statement expressions

Java switch statements are great for complex conditional logic, but the flood-gate approach to case processing is not intuitive. This shortcoming can make traditional switch statements error prone.

switch (playersPerSide) {
    case ONE, TWO -> System.out.println("Tennis");
    case THREE, FIVE -> System.out.println("Basketball");
    case SIX -> System.out.println("Hockey");
    case ELEVEN -> System.out.println(8);
}

The new arrow labels of the switch expression eliminate the fall-through functionality, which makes switch statements easier to read, write and debug. It also makes the flow logic of a switch statement much more intuitive for junior programmers.

Sealed classes

Inheritance is a powerful construct. But there are very few Java syntax checkpoints that limit the potential for inheritance-based, object-oriented corruption. The sealed Java class helps reign in the overzealous and often detrimental abuse of Java inheritance.

Sealed classes allow a developer to explicitly declare the names of the components that can legally inherit from a class they create. No longer is the inelegant use of an access modifier the only way to restrict the use of a superclass.

public abstract sealed class Shape 
            permits Annulus, Squircle, Triquetra { }

The key to the sealed class is using words sealed and permits in the class declaration.

Java 17 awaits

The Java 17 LTS release represents an evolution of the Java language. There are no giant changes to either the language's syntax or functionality.

But there are many little improvements that Java developers will love, all of which will help Java remain the best programming language to code in.

Dig Deeper on Core Java APIs and programming techniques