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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • How to Introduce a New API Quickly Using Micronaut
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

Trending

  • Rust, WASM, and Edge: Next-Level Performance
  • Enforcing Architecture With ArchUnit in Java
  • Monolith: The Good, The Bad and The Ugly
  • Event-Driven Microservices: How Kafka and RabbitMQ Power Scalable Systems
  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 user avatar
Sameer Shukla
DZone Core CORE ·
Dec. 12, 21 · Analysis
Likes (41)
Comment
Save
Tweet
Share
39.9K 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.

Related

  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • How to Introduce a New API Quickly Using Micronaut
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!