DZone
Java Zone
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
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > 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.

Sameer Shukla user avatar by
Sameer Shukla
CORE ·
Dec. 12, 21 · Java Zone · Analysis
Like (39)
Save
Tweet
23.02K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Popular on DZone

  • Suspicious Sortings in Unity, ASP.NET Core, and More
  • After Inspection, Comes Adaptation: How to Do Action-Based Retrospectives Right
  • Selenium vs. Protractor: What's the Difference?
  • Augmented Analytics: The Future of Business Intelligence

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo