Quick revision
The short answer
The equals method becomes return o instanceof Point p && x == p.x && y == p.y;. It is still correct for null because instanceof returns false for null, so the pattern does not match and the method returns false before p is used. The binding p is in scope after && because the right side runs only when the match succeeded. For non-final classes, the usual symmetry concerns of instanceof-based equals still apply, so the class should be final, compare getClass(), or simply be a record.
What to remember
In this answer, we rewrote equals as return o instanceof Point p && x == p.x && y == p.y;. It is correct for null because instanceof is false for null, so the method returns false before p is used, and p is in scope after && because flow scoping places it wherever the match succeeded. The rewrite does not fix the symmetry problem of instanceof-based equals in non-final classes, so we make the class final, compare getClass(), or use a record, and we keep hashCode consistent.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The equals method becomes return o instanceof Point p && x == p.x && y == p.y;. It is still correct for null because instanceof returns false for null, so the pattern does not match and the method returns false before p is used. The binding p is in scope after && because the right side runs only when the match succeeded. For non-final classes, the usual symmetry concerns of instanceof-based equals still apply, so the class should be final, compare getClass(), or simply be a record.
Why this question is asked
equals is one of the first methods most of us learned to write, and the classic version is a small ritual: check the type, cast, compare. Pattern matching for instanceof shrinks it to one expression. Interviewers ask this because the rewrite is easy, but explaining why it is still correct, for null and for subclasses, shows whether we understand both the pattern and the equals contract. In this answer, we will rewrite the method, walk through how it evaluates for each kind of argument, see why null is safe, and then look at the symmetry problem that pattern matching does not fix.
1The Original and the Rewrite
The original method tests the type, returns false if it does not match, casts into a new variable and compares the fields.
The type pattern o instanceof Point p checks the type and, if it matches, declares p already cast to Point. The whole method is now a single boolean expression. Notice that we can read p.x and p.y even if the fields are private, because the code is inside the Point class. And as always, equals and hashCode go together: two equal points must have the same hash code.
2How the Expression Evaluates
The && operator evaluates from left to right and stops as soon as one part is false. That short-circuiting is what makes the one-line version safe.
- o instanceof Point p?YesBind p, continueNoReturn false (covers null)
- x == p.x?YesContinueNoReturn false
- y == p.y?YesReturn trueNoReturn false
3 steps in order. Play them, or step through them one at a time.
Short-circuiting means p is used only after the match succeeded.
For an argument that is not a Point, the pattern does not match, the whole expression is false, and the field comparisons are never evaluated, so p is never used without a value. The binding p is in scope after the first && because of flow scoping: the compiler allows a binding to be used exactly where the match is known to have succeeded, and the right side of && runs only in that case.
3Why It Is Still Correct for null
The equals contract says that x.equals(null) must return false and must not throw. The old version handled that through its first line: !(null instanceof Point) is true, so it returned false. The new version relies on the same fact. instanceof is defined to return false when the value is null, whatever the type, and a type pattern simply never matches null. So there is no NullPointerException and no explicit null check is needed. Writing if (o == null) return false; in front would be harmless but redundant.
4The Symmetry Problem That Remains
Pattern matching changes the syntax, not the semantics. instanceof-based equals has always had a problem when the class can be subclassed and a subclass adds state. The equals contract requires symmetry: if a.equals(b) then b.equals(a).
Symmetry is broken, and collections behave inconsistently: a HashSet might or might not report a match depending on which object it compares from. There are three standard fixes. Make Point final, so no subclass can exist; this is the simplest and usually right for value types. Compare exact classes with o != null && o.getClass() == getClass(), which keeps symmetry but means a subclass instance is never equal to a parent instance, even when it adds no state. Or declare Point as a record, record Point(int x, int y) {}, which is final and gets a correct equals and hashCode generated, so we do not write either method.
5When Fields Are Not Integers
The rewrite pattern extends naturally to other field types, with one care point for each. For object fields, use Objects.equals(name, p.name), which handles null fields. For double and float fields, use Double.compare(d, p.d) == 0 rather than ==, so that NaN is equal to itself and the contract stays reflexive, which is also what records do.
6Follow-up questions
Should the new equals also check this == o first?
It is an optional optimisation. For a class with two int fields, comparing fields is already as cheap as the identity check. For classes with large collections or strings, adding o == this || at the front can save time when the same instance is compared, and it does not change the result.
Can we use a record pattern in equals?
Only if Point is a record, and then we should not write equals at all, because the generated one is correct. For a record in another context, o instanceof Point(int px, int py) && px == x && py == y would work, but it adds nothing here.
Does the getClass() approach break the Liskov substitution principle?
It can surprise callers, because a subclass instance that adds no state is never equal to an equal-looking parent instance, for example a Hibernate proxy subclass. That is one reason entity equals methods are usually based on ids with special handling for proxies, and value types are simply made final.
7Conclusion
In this answer, we rewrote equals as return o instanceof Point p && x == p.x && y == p.y;. It is correct for null because instanceof is false for null, so the method returns false before p is used, and p is in scope after && because flow scoping places it wherever the match succeeded. The rewrite does not fix the symmetry problem of instanceof-based equals in non-final classes, so we make the class final, compare getClass(), or use a record, and we keep hashCode consistent.
Open with
I would write it as a single expression: return o instanceof Point p && x == p.x && y == p.y. The type pattern checks that o is a Point and binds it to p already cast, so the separate cast line disappears.
Then, if they want more
It is still correct for null because instanceof is defined to return false for null, so the pattern does not match, the && short-circuits, and the method returns false without touching p.
No explicit null check is needed. p can be used after the first && because binding variables are flow scoped: they are in scope where the compiler knows the match succeeded, and the right side of && only runs in that case. Of course hashCode must still be overridden consistently, for example 31 times x plus y.
One thing the rewrite does not change is the old symmetry issue. If Point is not final and a subclass like ColorPoint adds a field and overrides equals, then point.equals(colorPoint) can be true while colorPoint.equals(point) is false, which breaks the equals contract and confuses hash-based collections.
The fixes are to make Point final, which is what I would do for a value type, to compare getClass() instead of using instanceof, or simply to make Point a record, which is final and gets a correct equals and hashCode generated. For double fields I would use Double.compare rather than equals with ==.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Simplify this equals method using instanceof patterns and explain why it is still...": https://scrivio-interview-library.netlify.app/java/sealed-and-pattern-matching/394f593414/