Implementing equals(Object) With instanceof Pattern Matching
See how pattern matching has changed.
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
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
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):
Goetz points out that this can now be written with a single statement, in this manner:
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
Being able to use
instanceof pattern matching to implement
equals(Object) with one fewer statement is a small improvement that is nevertheless welcome.
Published at DZone with permission of Dustin Marx, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.