Five new Java features to start using now

New features in Java

Java 17 adoption is increasing as organizations move away from Java 8 and Java 11 to migrate onto the latest LTS release.

Plenty has changed since Java 8 and Java 11 were first released, in 2014 and 2018 respectively. Developers should take this chance to brush up on their development skills and explore some of the new Java features in the latest version of the JDK .

Here are the top five new Java features developers should be excited to learn about:

  1. Multiline comments.
  2. Day period support.
  3. Pattern matching instanceof calls.
  4. List and array creation helpers.
  5. Introduction of the Record type.

Multiline Java Strings

The idea of a String that spans multiple lines has been around for decades, but unanticipated corner-cases that arose during the spec’s implementation delayed its release for years.

However, the multi-line String is now here. Just bookend a quote with three double-quotes and your String can span multiple lines:

String multiline = """
     Two roads diverged in a yellow wood,
     And sorry I could not travel both.
     """;
System.out.println(multiline);

Day-period support

Java’s day-period feature seems more amusing than applicable, but here’s how it works: if you pass the letter ‘B’ to the DateTimeFormatter, it spits out either in the morning, at night or in the afternoon.

For example, the following code prints out: 6:20 at night.

String currentDaySupport = DateTimeFormatter.ofPattern("h:m B").format(LocalTime.now());
System.out.println(currentDaySupport);

instanceof pattern matching

Historically, after an instanceof type-check, the next step is to cast the validated instance into the type that was checked, as in this example:

Object object = "text";
if (object instanceof String) {
  String text = (String)object;
  System.out.println(text.charAt(1));
}

With instanceof pattern matching, the need to cast goes away:

Object object = "text";
if (object instanceof String text) {
  //String text = (String)object;
  System.out.println(text.charAt(1));
}

Single line Java list initialization

A great deal of software development boils down to list manipulation. Developers who explore the new Java 17 features will love that there are three new ways to create a List in a single line of code:

  1. Use the Arrays’ asList() method
  2. Use the double bracket initialization feature
  3. Call the toList() method of a String

The Arrays’ asList() method lets you provide a comma-separated list of parameters, all of which are added to a new List:

List<String> phonetics = Arrays.asList("alpha", "beta", "charile");

This Java feature 17 improves upon another simplified list creation approach, the double bracket:

ArrayList<String> phonetics = new ArrayList<String>() {{
  add("alpha");
  add("beta");
  add("charlie");
}};

Additionally, you can turn a Java Stream into an unmodifiable list as well:

List<String> phoneticsC = Stream.of("alpha", "beta", "charile").toList();
Java 17 features

Java 17 features a number of benefits for developers and users alike.

The Java Record Type

The Java language hasn’t added a new reference type since Enums were introduced in Java 5, all the way back in 2004.

Java 14 in 2020 ushered in the Record type feature, and Java 17 is the first LTS release to support it.

The Java 17 Record class lets developers define an immutable data component that allows property access without the need to create setters or getters. This can greatly reduce verbosity and help eliminate tedious boilerplate code in modern programs.

Here’s an example of a simple Java record that represents the result of a Rock Paper Scissors game:

record Game(String clientGesture, String serverGesture, String result) { }

And here is the record used in code:

Game game = new Game("Paper", "Rock", "wins");
System.out.println(game.result()); //prints out 'wins'

Their syntax is similar in many ways to a prototypical Java class, so Java developers should pick it up quickly. The code’s lack of verbosity and ceremony makes learning Java easier for developers who are new to the language. It’s a win-win all around.

Learn Apache Maven fast!

Search App Architecture
Search Software Quality
Cloud Computing
Search Security
Search AWS
Close
  翻译: