DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL

Trending

  • Beyond the Wall of Charts: How Advanced Observability Displaced the Telemetry Dashboard
  • When Production Stops Moving: Running Claude Code Across a Distributed Enterprise Integration Team
  • Full-Text Search Meets the Graph: Apache Lucene Inside Neo4j
  • You Do Not Need a PhD to Become an AI Engineer: The Essential Skill Stack

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. Java: Migrating From Java 8 to Java 17

Java: Migrating From Java 8 to Java 17

In this article, I will highlight some extremely important features available which can be learned and implemented quickly when migrating from directly Java 8 to Java 17.

By 
Sameer Shukla
DZone Core CORE ·
Dec. 12, 21 · Analysis
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
40.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

This article focuses on the features to use when moving directly from Java 8 to Java 17. If your application is still running on Java 8 in production, then It’s difficult to track the features developed after version 8 onwards but there are quite a few easy to use and extremely important features available which can be learned and implemented quickly when migrating from directly Java 8 to Java 17. 

In this article I will try to highlight some of them, I will also try to highlight the versions on which the specific feature is available for us to use but not necessarily in the sequential order of release.

NullPointerException Variable Identification

Precise identification of variables which was null when the NullPointerException is generated by JVM. All we need to do is to add “XX:+ShowCodeDetailsInExceptionMessages” to enable this feature. Let’s understand by a simple example:


NullPointerException example

Results in:

NullPointerException result

Released version: Java 14

Switch Expression

From Java 14 onwards, switch expression can return values, this was the preview feature in 12 and 13. This enhancement is extremely important from a Pattern Matching perspective, all the features which have been released in every release are the building blocks of a complete feature that will be released in the future. For example, this feature is the building block for Pattern matching which is a preview feature as of Java 17.

Switch expression feature

Please note: 

  • The ‘yield’ keyword should be used to return the value from the switch expression.
  • All the blocks inside every case statement are multiline blocks. 

 Released version: Java 14

Record Type

Record types are the container classes that come with implicit getters/setters, constructor, equals/hashcode, and toString methods, also record container classes are Immutable in nature and are identical to Lombok. If your application is running on Java 16 onwards you can easily use record classes as DTO’s, to begin with. Use the keyword ‘record’ to create the record classes.

Record type feature


Released version: Java 16

The Enhanced instanceof Operator

Prior to Java 16 we must check the type of Object first then we have to cast the object to a variable, but the enhanced ‘instanceof’ can check the type and implicitly cast the object to a variable. I feel this enhancement is required for ‘Pattern Guards’, in the pattern matching section we will see how enhanced instanceof operator will play a role. 

Prior to Java 16:

Object casting prior to Java 16


Java 16 onwards:

Released version: Java 16

CompactNumberFormat

CompactNumberFormat class is the subclass of NumberFormat class in java.text package, NumberFormat class formats a number in compact form. 

The NumberFormat Style, SHORT would display 1000 as 1K and 10000 as 10K. Whereas the Style LONG will display 1000 as 1 thousand and 10000 as 10 thousand.  

CompactNumberFormat class

Output: 1K

CompactNumberFormat class

Output: 1 thousand

CompactNumberFormat class

Similarly, the parse method should be used to parse the compact number to a long  pattern

CompactNumberFormat class

Released version: Java 12

Pattern Matching

As of Java 17, Pattern matching is a preview feature. To understand and execute this feature use –enable-preview option, or if you are an Eclipse user then the “Enable the preview features” option. 

Two features that we saw earlier, “Switch Expression” and “Enhanced Instanceof Operator” were the building blocks for the Pattern matching feature. First, we will execute a simple program using the Switch Expression then will utilize the “Enhanced Instanceof” as a Pattern Guard in our program. 

With the enhanced pattern matching coming up in the upcoming versions, we can avoid using instanceof and instead can write:

Since we are passing double the result is Double. If we pass integer 10 then the output will be Integer. 

The enhanced ‘instanceof’ can check the type and implicitly cast the object to a variable, the same feature now can be used for pattern guard. 

We have put a guard in the Integer block, that returns “Integer” only if the value is greater than 20.

 

Released version: Java 17 (preview) Java 18 (complete)

Text Block

From Java 15 onwards text block is available to use, with Text Block we can easily express multi-line Strings which enhances the code readability. Text block uses tripe quotes (“””). 

Prior to Java 15: 

Multi-line string expression prior to Java 15

Java 15 onwards: 

Multi-line string expression from Java 15 onwards

Released version: Java 15

New String Methods

In Java 11 and 12 versions, there are certain new String methods are added that are extremely useful. 

repeat

11

isBlank

11

strip

11

lines

11

indent

12

transform

12


I hope you like the article, this is the first of the Series, in further articles I will try to cover more such features.

Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook