Quick revision
The short answer
Arrays do not override equals() or toString(), so they inherit Object's versions: equals compares references, and x and y are two different array objects, so it prints false, while toString prints the JVM's type name and a hash, something like [I@1b6d3586. To compare and print contents we call Arrays.equals(x, y) and Arrays.toString(x), the deep variants deepEquals and deepToString for nested arrays, and Arrays.hashCode or deepHashCode to match; an array used as a HashMap key is compared by identity, so we wrap it in a List or a record with proper equals.
What to remember
In this answer, we saw that arrays inherit equals, hashCode and toString from Object, so x.equals(y) compares references and prints false, and printing an array shows its JVM type name and identity hash, such as [I@1b6d3586. Arrays.equals and Arrays.toString work on contents, deepEquals and deepToString handle nested arrays, and Arrays.hashCode and deepHashCode keep hash codes consistent with them. An array used as a HashMap key, even inside a record, is compared by identity, so we use a List, a record with named fields, or explicit equals and hashCode.
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.
Arrays do not override equals() or toString(), so they inherit Object's versions: equals compares references, and x and y are two different array objects, so it prints false, while toString prints the JVM's type name and a hash, something like [I@1b6d3586. To compare and print contents we call Arrays.equals(x, y) and Arrays.toString(x), the deep variants deepEquals and deepToString for nested arrays, and Arrays.hashCode or deepHashCode to match; an array used as a HashMap key is compared by identity, so we wrap it in a List or a record with proper equals.
Why this question is asked
This is one of the first surprises Java developers meet: two arrays with the same contents are not equal, and printing one shows gibberish. It matters beyond beginner code, because the same rule makes arrays silently misbehave as map keys, in sets, in assertions and inside records. Interviewers ask it to check that we know which methods arrays actually have and where the helpers live. In this answer, we will see why the code prints what it prints, then compare and print contents correctly, handle nested arrays, keep hash codes consistent, and fix the map key problem.
1Arrays Inherit equals and toString From Object
Every array is an object, and its class, such as int[], is created by the JVM rather than written in Java source. That class adds nothing beyond the length field and clone(), so equals(), hashCode() and toString() are the versions from java.lang.Object. Object.equals is identity: it returns true only when both references point to the same object. Object.toString returns the class name, an @ sign and the hash code in hexadecimal.
So x.equals(y) is false because x and y are two separate array objects that merely contain the same values. The strange text [I is the JVM's internal name for the class int[]: an opening bracket for each array dimension and a letter for the element type, I for int. The part after @ is the identity hash code, which differs between runs.
2Comparing and Printing Contents
The helpers we need live in java.util.Arrays. Arrays.equals(a, b) is true when both arrays have the same length and equal elements at each index, and Arrays.toString(a) prints the elements in brackets. Both have overloads for every primitive type and for Object[].
Arrays.mismatch and Arrays.compare, added in Java 9, are useful for diagnostics and ordering, since they say where two arrays differ and which one is smaller. In tests, JUnit's assertArrayEquals compares contents in the same way and prints the differing index when it fails, which is much clearer than assertTrue(Arrays.equals(...)).
3Nested Arrays Need the Deep Variants
A two-dimensional array in Java is an array whose elements are other arrays. Arrays.equals on such an array compares the elements with their own equals method, which for the inner arrays is identity again, so it returns false. Arrays.deepEquals and Arrays.deepToString look inside nested arrays recursively.
| Job | Flat arrays | Nested arrays |
|---|---|---|
| Compare contents | Arrays.equals(a, b) | Arrays.deepEquals(a, b) |
| Print contents | Arrays.toString(a) | Arrays.deepToString(a) |
| Hash code from contents | Arrays.hashCode(a) | Arrays.deepHashCode(a) |
What a.equals(b) does | Identity, like == | Identity, like == |
The rule is easy to remember: whenever elements may themselves be arrays, which includes an Object[] that might contain arrays, use the deep version.
4Keeping Hash Codes Consistent
Whenever two objects are equal, their hash codes must be equal, or hash-based collections break. Since array equality by content is our own choice, we must pair it with a content-based hash: Arrays.hashCode for flat arrays and Arrays.deepHashCode for nested ones. They are defined to give the same result as the hash code of a List with the same elements, so Arrays.hashCode(new int[] {1, 2}) and List.of(1, 2).hashCode() are both 994. If we write a class that holds an array field, its equals should use Arrays.equals and its hashCode should use Arrays.hashCode on that field.
5Arrays as HashMap Keys
Because an array's equals and hashCode are identity-based, using an array as a map key works only if we look it up with the very same array object. A new array with the same values is a different key.
Notice the last case. A record generates equals and hashCode from its components, but for an array component it uses the array's own equals, which is identity. So a record wrapping an array needs explicit equals and hashCode based on Arrays.equals and Arrays.hashCode, and it should copy the array in its constructor, because a caller who changes the array after using it as a key would change the key's hash and lose the entry. In most cases a List or a record with named fields is the simpler key.
6Follow-up questions
Why did Java not give arrays a content-based equals?
Arrays were designed in Java 1.0 as low-level, mutable containers, and identity equality is the safe default for mutable objects: if two arrays compared equal by content, changing one would silently change the result of earlier comparisons and break any hash set that contained it. Changing it now would break the behaviour of existing code, so the content-based helpers were added in java.util.Arrays in Java 1.2 instead. Collections such as List, designed later, define equals by contents from the start.
What does [Ljava.lang.String;@... mean when I print a String array?
It is the same Object.toString output for a String[]. The JVM's name for an array class starts with one [ per dimension, followed by a letter for a primitive element type, such as I for int, J for long or Z for boolean, or by L, the fully qualified class name and a semicolon for a reference type. So [Ljava.lang.String; is String[] and [[I is int[][]. Arrays.toString or deepToString prints the contents instead.
Does assertEquals work for arrays in JUnit?
Not for contents. assertEquals(expected, actual) with two arrays calls equals, which is identity, so it fails for two arrays with the same values. JUnit provides assertArrayEquals for that, which compares element by element, handles nested arrays, and reports the first index that differs. AssertJ's assertThat(actual).containsExactly(...) is another readable option.
7Conclusion
In this answer, we saw that arrays inherit equals, hashCode and toString from Object, so x.equals(y) compares references and prints false, and printing an array shows its JVM type name and identity hash, such as [I@1b6d3586. Arrays.equals and Arrays.toString work on contents, deepEquals and deepToString handle nested arrays, and Arrays.hashCode and deepHashCode keep hash codes consistent with them. An array used as a HashMap key, even inside a record, is compared by identity, so we use a List, a record with named fields, or explicit equals and hashCode.
Open with
It prints false followed by something like [I@1b6d3586. Arrays don't override equals or toString, so they get Object's versions. equals is identity, and x and y are two separate array objects that just happen to contain the same values, so it's false. toString prints the class name, an at sign and the identity hash in hex; [I is the JVM's name for int array, one bracket per dimension and I for int.
Then, if they want more
To compare contents I call Arrays.equals, which checks the length and each element, and to print them Arrays.toString, which gives 1, 2 in brackets. For nested arrays, the plain versions only look one level deep and compare the inner arrays by identity, so I use Arrays.deepEquals and Arrays.deepToString.
Hash codes follow the same split: Arrays.hashCode and deepHashCode give content-based hashes that are consistent with those equals methods, and they match the hash of a List with the same elements.
The practical trap is using an array as a HashMap key or in a HashSet: it's identity-based, so looking it up with a new array with the same values returns null. I'd use a List or, better, a record with named fields as the key.
And note that a record that wraps an array has the same problem, because its generated equals uses the array's own equals, so it needs explicit equals and hashCode with Arrays.equals and Arrays.hashCode, plus a defensive copy. In tests, I'd use assertArrayEquals rather than assertEquals.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 9
- 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 "Why does this print false and what should you call instead?": https://scrivio-interview-library.netlify.app/java/arrays/8cc0451693/