equals vs Arrays.equals in Java
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Java has two very different equality stories: object equality through equals, and array content equality through helpers in java.util.Arrays. Confusion usually happens because arrays are objects, but their inherited equals method still compares references rather than elements.
What Plain equals Does
Every Java class inherits equals from Object. If a class does not override it, the default behavior is reference equality.
Both lines print false because a and b refer to different array instances.
For ordinary domain objects, developers often override equals to describe logical identity.
That is the normal use of equals: defining value semantics for a class you control.
Why Arrays Need Arrays.equals
Arrays are special because you do not override their methods. If you want element-by-element comparison, use Arrays.equals.
Arrays.equals walks both arrays in order and compares corresponding elements. For primitive arrays it compares raw values. For object arrays it calls equals on each element.
This means the correctness of object-array comparison depends on the element type implementing equals sensibly.
Use Arrays.deepEquals for Nested Arrays
Arrays.equals is still shallow for nested arrays. If the elements are themselves arrays, use Arrays.deepEquals.
This distinction matters in matrix code, parsed JSON-like structures, and tests that compare nested data.
Pick the Right Equality Tool
A simple rule helps:
- compare ordinary objects with their
equalsimplementation - compare one-dimensional arrays with
Arrays.equals - compare nested arrays with
Arrays.deepEquals
If you find yourself repeatedly comparing arrays inside a domain type, it may be cleaner for that type to implement its own equals and delegate to Arrays.equals internally.
That keeps equality logic close to the type that owns the data.
Common Pitfalls
- Calling
array.equals(otherArray)and expecting element comparison returns reference equality instead. - Overriding
equalswithout also overridinghashCodebreaks collections such asHashMapandHashSet. - Using
Arrays.equalson nested arrays gives surprisingfalseresults because the comparison is shallow. - Forgetting that object-array comparison depends on element
equalsbehavior leads to inconsistent results. - Using
==for strings or boxed values compares references, not logical contents.
Summary
- Default
equalsfromObjectcompares references unless a class overrides it. - Arrays do not override
equals, soarray.equals(...)is usually the wrong choice. - Use
Arrays.equalsfor one-dimensional array content comparison. - Use
Arrays.deepEqualsfor nested arrays. - When arrays are part of a value object, delegate to
Arrays.equalsandArrays.hashCodeinside that type.
Related reading
- Equivalence classes and union/find in a functional language
- Errata in the original paper on suffix arrays?
- Error AccessControlListNotSupported when trying to create a bucket ACL in AWS
- Error dictionary update sequence element 0 has length 1; 2 is required on Django 1.4
- Error1, 0 Plugin with id ''com.android.application'' not found
- Error - is not marked as serializable
- Error Expected 2D array, got 1D array instead Using OneHotEncoder
- Error in Python script Expected 2D array, got 1D array instead?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.