Detailed explanation
The output is false, false, and true, each on a separate line. Although a and b contain the same integers in the same order, the two array initialisers create distinct array objects. Each variable holds a reference to its own object. This distinction between object identity and content equality is the central concept being tested. The code assumes that java.util.Arrays has been imported, or that the final call uses the fully qualified name java.util.Arrays.equals(a, b).
The expression a == b compares references because both operands are array reference variables. It asks whether the variables refer to the exact same array object, not whether their elements match. Since the arrays were created separately, the result is false. The expression a.equals(b) also returns false: Java arrays inherit equals(Object) from Object and do not override it to provide content comparison. Object.equals uses identity equality. Therefore, calling equals on an array does not automatically inspect its contents, unlike calling equals on a String or a List.
Arrays.equals(a, b) selects the overload for two int[] arguments. Its contract compares array contents: two arrays are equal when they have the same length and corresponding integer elements are equal. Here, both arrays have length three, and every corresponding pair matches, so the result is true. The comparison is order-sensitive; {1, 2, 3} and {3, 2, 1} would not be equal. It can return immediately for identical references, null mismatches, or unequal lengths; otherwise, content checking takes O(n) time in the worst case.
Interviewers use this example to check whether you distinguish an operator, an inherited instance method, and a static utility method. If b = a were executed, all three comparisons would return true because both variables would reference the same object. For nested arrays, Arrays.equals does not recursively compare the contents of inner arrays; use Arrays.deepEquals for that purpose. Also remember that a.equals(b) throws NullPointerException if a is null, whereas Arrays.equals safely handles null arguments.
Key points to remember
- The output is false, false, true on three separate lines.
- For array variables, == compares object identity rather than elements.
- Arrays inherit Object.equals, so a.equals(b) also checks identity.
- Arrays.equals(int[], int[]) checks equal lengths and matching elements in order.
- Import java.util.Arrays; use Arrays.deepEquals for recursive comparison of nested arrays.
Common mistakes candidates make
- Assuming identical array initialisers produce references to the same object.
- Expecting array.equals() to compare contents because String.equals() does.
- Using Arrays.equals on nested arrays and expecting recursive comparison of inner-array contents.
Likely follow-up questions
What changes if b is assigned a instead of being initialised separately?
All three comparisons return true because both variables reference the same array. A change made through either variable is visible through the other.
How does Arrays.equals handle null arrays?
For int[] variables, Arrays.equals returns true if both are null and false if exactly one is null. Calling a.equals(b) throws NullPointerException when a is null.
How should two int[][] arrays be compared by content?
Use Arrays.deepEquals to recursively compare their inner arrays. Arrays.equals only compares the outer elements using their equals methods, so separate inner int[] objects are compared by identity.
Interview tip: State the three output lines first, then explain identity versus content equality and why each expression uses one or the other.
