New Features in Java 17
JAVA 17 features

New Features in Java 17

In this article, I will share the Java SE 17 new features and changes. Java 17 LTS is the long-term support release for the Java SE platform. It was released on September 15, 2021.

First, there are many JDK Enhancement Proposals (JEPs) in Java 17. I mention them in order of JEP numbers, you can see the list below:

  • JEP 306: Restore Always-Strict Floating-Point Semantics
  • JEP 356: Enhanced Pseudo-Random Number Generators
  • JEP 382: New macOS Rendering Pipeline
  • JEP 391: macOS/AArch64 Port
  • JEP 398: Deprecate the Applet API for Removal
  • JEP 403: Strongly Encapsulate JDK Internals
  • JEP 406: Pattern Matching for a switch (Preview)
  • JEP 407: Remove RMI Activation
  • JEP 409: Sealed Classes
  • JEP 410: Remove the Experimental AOT and JIT Compiler
  • JEP 411: Deprecate the Security Manager for Removal
  • JEP 412: Foreign Function & Memory API (Incubator)
  • JEP 414: Vector API (Second incubator)
  • JEP 415: Context-Specific Deserialization Filters

As for now, let’s analyze the changes and new features in each iteration of Java, that are most important from the perspective of most of us Java Developers.

Record: Records are very useful for immutable data carrier classes.?

  • They are immutable.
  • Their fields are private and final.
  • Record Class defines canonical constructors for all fields.
  • All fields have Getters, equals, hashCode, and toString.

Let’s declare a Record as BankAccount below:

No alt text provided for this image

With the record declaration above, we automatically define:

  • Private final fields for bankName and accountNumber.
  • Canonical constructors for all fields.
  • Getters for all fields.
  • equals, hashCode, and toString for all fields.

We can also do the following for the records;

  • We can define additional methods.
  • Implement interfaces.
  • Customize the canonical constructor and accessors.

Sealed Classes

The sealed classes?permit their subclasses to extend them. The other classes cannot extend the parent if they are not permitted an extension by the parent class.?The sealed interfaces?permit the subinterfaces and implementing classes.?All permitted classes or interfaces in the “permits list” must be declared as final and they needed to be located in the same package

No alt text provided for this image

Text Blocks

Let’s start with the Text blocks definition. Text Blocks are blocks of String that can be declared by starting with three double quotes “”” which can be followed by a line break and closed by three double quotes again. We can use newlines and quotes inside the text blocks without taking care of escape line break characters and in this way it will be much easier and readable to work with JSON, SQL, and similar texts with text blocks.

No alt text provided for this image

Text blocks can be used to keep a reasonably readable JSON or XML template in your code. External files are still likely a better idea, but it’s still a nice option to do it in pure Java if necessary.

Switch Expressions

?is present in a lot of languages, but over the years it got less and less useful because of the limitations it had. Other parts of Java grew,?but the switch?did not. Nowadays?switch?cases can be grouped much more easily and in a more readable manner (note there’s no?break!) and the?switch?expression itself actually returns a result.

No alt text provided for this image

Even more, can be achieved with the new?yield?keyword that allows returning a value from inside a code block. It’s virtually a?return?that works from inside a case block and sets that value as a result of its?switch. It can also accept an expression instead of a single value. Let’s take a look at an example:

No alt text provided for this image

Context-Specific Deserialization Filters

As everyone knows, deserializing the data from untrusted data is dangerous and should not be a best practice. Java 9, enabled software engineers to validate incoming serialized data from untrusted sources to prevent security issues. Java 17, allows applications to configure context-specific and dynamically selected deserialization filters defined at the JVM level.

Vector API (Second Incubator)

Java 17, leverages specialized CPU hardware that supports vector instructions and allows the execution of such instructions as pipelines. This improves the Vector API performance and also adds other enhancements like support operations on characters, translating byte vectors to and from boolean arrays, etc.

If you are starting a project from scratch and thinking about a Java version to use might want to consider Java 17, as it’s more secure and contains useful features. It’s also an LTS release that has a longer support period, so it might be considered a future-proof option. To decide one must also think about dependencies in the project if any of the frameworks planned to use support Java 17 release.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了