New Language Features in Java 17 | TechWell

New Language Features in Java 17

New Language Features in Java 17

As Java 17 is the latest Long Term Support (LTS) version after Java 11, it is opportune to go over the new language features it adds, some of which had been Preview features in intermediate versions.

Text Blocks

String literals had shortcomings, and text blocks enhanced string literals with some new characteristics:

  • Text blocks allow a string literal to span over multiple lines without using double quotes (“), concatenation operator (+), and escape sequence for the newline character.
  • Text blocks automatically format string literals.
  • Text blocks are a two-dimensional representation of text.

Sealed Classes

Sealed classes provide a more fine-grained control over extensibility of classes. Earlier, a class could either be package-private (default), or public. A sealed class is declared with the sealed modifier, and the class declaration also includes the classes that are allowed to extend it with the permits modifier.

What are its benefits? Some are:

  • Modular code suitable for internal classes and proprietary software
  • Simpler software development as end user is known in advance

Records

Records is a new feature for declaring data carriers. A data carrier, or aggregate gets several benefits:

  • Concise syntax without excessive boilerplate code. The new keyword record is all that is needed.
  • Model data as data.
  • State description that declares the components of the record, the data holders.
  • Optionally a body declared with curly braces {}.

Switch Expressions

  • A new form of the switch label as case L -> switch overcomes the shortcomings of the traditional switch statement :
  • Non fall-through semantics with which none of the statements following a matching case label are run, precluding the use of a break statement.
  • The default scoping of a switch label is only the label. Only the right of a switch label is run, which may be one of the following:
    • An expression
    • A block denoted with {}
    • A throw statement
  • The switch may be used as a switch expression in which a variable is assigned the expression value.

Pattern Matching for Instanceof

The instanceof operator is used to test if an object is a certain data type. The instanceof matches the target object to a declared type for conditional processing of a subsequent block of code. If the object is not of the type it is being tested against, the subsequent block is never run and becomes unnecessary code. In the new pattern matching feature, a type test pattern is used for matching instead of a type. A type test pattern consists of a predicate that specifies a type and a binding variable. The instanceof tests the predicate with the target object and only if the predicate applies to the target object successfully the binding variable/s is/are extracted from the object.

Always Strict Floating Point Semantics

It’s not really a new feature, but a hark back to Java SE 1.1, floating point semantics are restored to being strict by default to take advantage of the latter day Pentium processors, which are able to support strict JVM floating point operations without incurring undue overhead.

Get used to the new language features, as the next LTS Java 23 will be released in about one and a half years in September 2023!

Up Next

About the Author

TechWell Insights To Go

(* Required fields)

Get the latest stories delivered to your inbox every week.