top of page
Search

Java 14 - What's New?

  • schick09
  • Jul 12
  • 2 min read

Java 14, officially released on March 17, 2020, marked another strong step in the six-month release cadence initiated by Oracle. While it’s not a long-term support (LTS) release, it brought several exciting preview and production-ready features that improve developer productivity, code clarity, and the overall Java experience.

In this post, I’ll walk you through all the key features introduced in Java 14, including both incubating APIs and preview features, with code examples where applicable.


Switch Expressions


Java 14 finalizes switch expressions, which were previewed in JDK 12 and 13. This new form allows switch to be used as both a statement and an expression, bringing greater flexibility and less boilerplate.


int day = 3;

String dayType = switch (day) {

case 1, 2, 3, 4, 5 -> "Weekday";

case 6, 7 -> "Weekend";

default -> throw new IllegalArgumentException("Invalid day: " + day);

};


Records (Preview Feature)


Records are a preview feature that provide a concise syntax for declaring immutable data classes. Think of it as a quick way to create POJOs without boilerplate.


public record Point(int x, int y) {}


Point p = new Point(10, 20);

System.out.println(p); // Output: Point[x=10, y=20]



Pattern Matching for instanceof (Preview Feature)


No more boilerplate type casting after an instanceof check! This preview feature simplifies the pattern of testing and casting in a single step.


if (obj instanceof String s) {

System.out.println(s.toLowerCase());

}


...where you used to have to do this:


if (obj instanceof String) {

String s = (String) obj;

System.out.println(s.toLowerCase());

}




Text Blocks


Text Blocks simplify working with multi-line strings, which was previously messy with escape characters and concatenation.


String html = """

<html>

<body>

<p>Hello, world</p>

</body>

</html>

""";


...which is much cleaner than the old way:


String html = "<html>\n" +

" <body>\n" +

" <p>Hello, world</p>\n" +

" </body>\n" +

"</html>";


ree


 
 
 

Comments


Dave Schick Consulting

©2023 by Dave Schick Consulting. Proudly created with Wix.com

bottom of page