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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Yet 4 More Techniques for Writing Better Java
  • Injecting Implementations With Jakarta CDI Using Polymorphism
  • Jakarta WebSocket Essentials: A Guide to Full-Duplex Communication in Java
  • Java Is Greener on Arm

Trending

  • Unlocking AI Coding Assistants Part 3: Generating Diagrams, Open API Specs, And Test Data
  • Article Moderation: Your Questions, Answered
  • Building Resilient Networks: Limiting the Risk and Scope of Cyber Attacks
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  1. DZone
  2. Coding
  3. Java
  4. Implementing equals(Object) With instanceof Pattern Matching

Implementing equals(Object) With instanceof Pattern Matching

See how pattern matching has changed.

By 
Dustin Marx user avatar
Dustin Marx
·
Mar. 27, 21 · News
Likes (1)
Comment
Save
Tweet
Share
5.8K Views

Join the DZone community and get the full member experience.

Join For Free

Pattern matching for the instanceof operator was introduced as a preview feature with JDK 14 and was finalized with JDK 16. Because instanceof pattern matching is finalized for JDK 16, it is not surprising to now see changes being made to the JDK to take advantage of pattern matching for the instanceof operator. These changes to the JDK to leverage instanceof pattern matching can provide ideas and examples for where to begin applying this in our own code. In this post, I look at the use of instanceof pattern matching in the implementation of the ubiquitous equals(Object) methods.

In a message posted to the core-libs-dev OpenJDK mailing list related to a code review for JDK-8263358 ("Update java.lang to use instanceof pattern variable"), Brian Goetz provided a reminder that a standard approach used in implementation of equals(Object) can now be modified to take advantage of pattern matching for instanceof.

In the message, Goetz uses an example of how we have often implemented equals(Object) (but using instanceof pattern matching in this example consistent with the past):

Java
 




xxxxxxxxxx
1


 
1
if (!(o instanceof Key that)) return false;  
2
return name == that.name && Arrays.equals(ptypes, that.ptypes); 



Goetz points out that this can now be written with a single statement, in this manner:

Java
 




x


 
1
return (o instanceof Key that)  
2
   && name == that.name  
3
   && Arrays.equals(ptypes, that.ptypes);  



Goetz's message concludes with this:

The use of "if it's not, return false" is a holdover from when we couldn't express this as a single expression (which is almost always preferable), which means we had to fall back to control flow. Now we don't have to.

A new commit was made based on Goetz's feedback and that commit is Commit e9d913152cefda827e01c060a13f15eacc086b33. One can review the several changes associated with this commit to see multiple statements converted into single statements in the various equals(Object) methods.

Being able to use instanceof pattern matching to implement equals(Object) with one fewer statement is a small improvement that is nevertheless welcome.

Java (programming language) Java Development Kit Commit (data management) Express Advantage (cryptography) Implementation Operator (extension) Leverage (statistics) OpenJDK

Published at DZone with permission of Dustin Marx, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Yet 4 More Techniques for Writing Better Java
  • Injecting Implementations With Jakarta CDI Using Polymorphism
  • Jakarta WebSocket Essentials: A Guide to Full-Duplex Communication in Java
  • Java Is Greener on Arm

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!