DEV Community

Cover image for Upgrading from Java 11 to Java 17
Benjamin Rancourt
Benjamin Rancourt

Posted on • Originally published at benjaminrancourt.ca on

Upgrading from Java 11 to Java 17

With the release of Java 17 on September 14, 2021, those who use only Long Term Support (LTS) versions will be thrilled to update their Java version! But to use the new functionalities that come with a new version, we must know what are they. 😉

The list below is the ongoing new features that I think will be useful to my colleagues or myself. It doesn't contain any advanced features (or deep), so if you want to know them, check all the release notes. 😘

Text indentation

Java 12 introduces a new String function: indent and as its name says, it helps to indent a string with spaces.

// From older versions
final String version = "Java 12";
final String release = " There is a new indent function.";
System.out.println(version);
System.out.println(release);

// Java 12 +
final String version = "Java 12";
final String release = "There is a new indent function.".indent(2);
System.out.println(version);
System.out.println(release);
Enter fullscreen mode Exit fullscreen mode

Both versions will produce an output that looks like this:

Java 12
  There is a new indent function.
Enter fullscreen mode Exit fullscreen mode
The output text from the previous lines of code.

Text Blocks

Since Java 15, multi-line strings , more know as Text Blocks in the Java world, are available.

// Before
final String description =
  "Une ou plusieurs exceptions sont survenues durant l'exécution de la passerelle."
  + NEW_LINE
  + NEW_LINE
  + " **Veuillez vérifier le bon fonctionnement de celle-ci et adaptez le code en conséquence.**";

// After
 final String description =
   """
     Une ou plusieurs exceptions sont survenues durant l'exécution de la passerelle.

     **Veuillez vérifier le bon fonctionnement de celle-ci et adaptez le code en conséquence.**
   """;

// Before
notes.add(
        " **" + note + "**" + NEW_LINE + ("```

" + getStackTrace(exception) + "

```") + NEW_LINE);

// After
notes.add(
  """
    **%s**
    %s
  """.formatted(note, getStackTrace(exception)));
Enter fullscreen mode Exit fullscreen mode

It was one of the features that I was looking forward to, as it is available for a long time in JavaScript. 😎

It is much cleaner, right? 😉

Switch Expressions

For this one, I'm am not sure about the readability, but it could prove useful to know someday that there is a way to write succinct switch expressions in Java 14:

DayOfWeek dayOfWeek = LocalDate.now().getDayOfWeek();
boolean isTodayHoliday = switch (day) {
    case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> false;
    case SATURDAY, SUNDAY -> true;
    default -> throw new IllegalArgumentException("What's a " + day);
};
Enter fullscreen mode Exit fullscreen mode

Helpful NullPointerExceptions

Ever had a NullPointerException? Before Java 14, we needed to debug the code to know exactly what variable had caused the exception. Now, the log will be more precise and will say which variable caused the exception.

// Before
Exception in thread "main" java.lang.NullPointerException
at com.baeldung.MyClass.main(MyClass.java:27)

// After
java.lang.NullPointerException: Cannot store to int array because "a" is null
Enter fullscreen mode Exit fullscreen mode

I can't wait to see the next time I run across this exception. 🤣

Conclusion

If you find other features that others and I should learn, let me know! 🤘

Do you plan to upgrade your Java applications to the latest LTS version?

Top comments (0)