array1.equals(array2) is the same as array1 == array2, i.e. is it the same array. As @alf points out it's not what most people expect.

Arrays.equals(array1, array2) compares the contents of the arrays.


Similarly array.toString() may not be very useful and you need to use Arrays.toString(array).

Answer from Peter Lawrey on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_arrays_equals.asp
Java Arrays equals() Method
The equals() method checks whether two arrays are equal.
Top answer
1 of 9
352

array1.equals(array2) is the same as array1 == array2, i.e. is it the same array. As @alf points out it's not what most people expect.

Arrays.equals(array1, array2) compares the contents of the arrays.


Similarly array.toString() may not be very useful and you need to use Arrays.toString(array).

2 of 9
101

It's an infamous problem: .equals() for arrays is badly broken, just don't use it, ever.

That said, it's not "broken" as in "someone has done it in a really wrong way" — it's just doing what's defined and not what's usually expected. So for purists: it's perfectly fine, and that also means, don't use it, ever.

Now the expected behaviour for equals is to compare data. The default behaviour is to compare the identity, as Object does not have any data (for purists: yes it has, but it's not the point); assumption is, if you need equals in subclasses, you'll implement it. In arrays, there's no implementation for you, so you're not supposed to use it.

So the difference is, Arrays.equals(array1, array2) works as you would expect (i.e. compares content), array1.equals(array2) falls back to Object.equals implementation, which in turn compares identity, and thus better replaced by == (for purists: yes I know about null).

Problem is, even Arrays.equals(array1, array2) will bite you hard if elements of array do not implement equals properly. It's a very naive statement, I know, but there's a very important less-than-obvious case: consider a 2D array.

2D array in Java is an array of arrays, and arrays' equals is broken (or useless if you prefer), so Arrays.equals(array1, array2) will not work as you expect on 2D arrays.

🌐
GeeksforGeeks
geeksforgeeks.org › java › java-util-arrays-equals-java-examples
Arrays.equals() in Java with Examples - GeeksforGeeks
It is used to check whether two arrays, whether single-dimensional or multi-dimensional array are equal or not. ... import java.util.Arrays; public class ArraysEquals { public static void main(String[] args) { // create different integers arrays ...
Published: April 13, 2026
🌐
DataCamp
datacamp.com › doc › java › equals
Java Array equals()
This method checks whether two arrays are equal by comparing the elements in both arrays in sequence.
🌐
TutorialsPoint
tutorialspoint.com › java › util › arrays_equals_int.htm
Java Arrays equals(int[], int[]) Method
The Java Arrays equals(int[] a, int[] a2) method returns true if the two specified arrays of ints are equal to one another. Two arrays are equal if they contain the same elements in the same order.
🌐
Medium
medium.com › @AlexanderObregon › javas-arrays-equals-method-explained-271f9420177d
Java’s Arrays.equals() Method Explained | Medium
September 11, 2024 - In this case, since array1 and ... to be equal. If the arrays are of the same length, Arrays.equals() proceeds to compare the elements of the arrays one by one....
🌐
TutorialsPoint
tutorialspoint.com › java › util › arrays_equals_object.htm
Java Arrays equals(Object[], Object[]) Method
The Java Arrays equals(Object[] a, Object[] a2) method returns true if the two specified arrays of objects are equal to one another. Two objects e1 and e2 are considered equal if Objects.equals(e1, e2).
🌐
Tutorial Gateway
tutorialgateway.org › java-array-equals-method
Java Array equals Method
March 28, 2025 - The Java Array equals Method check whether user specified arrays are same or not & if they are equal it return Boolean TRUE otherwise FALSE.
Find elsewhere
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.util.arrays.equals
Arrays.Equals Method (Java.Util) | Microsoft Learn
Returns true if the two specified arrays of bytes, over the specified ranges, are equal to one another.
🌐
TutorialsPoint
tutorialspoint.com › java › util › arrays_equals_char.htm
Java Arrays equals(char[], char[]) Method
The Java Arrays equals(char[] a, char[] a2) method returns true if the two specified arrays of chars are equal to one another. Two arrays are equal if they contain the same elements in the same order.
🌐
DataCamp
datacamp.com › doc › java › compare
Java Array compare()
While Java does not have a built-in compare() method specifically for arrays, it provides utility methods in the java.util.Arrays class to facilitate array comparison. The Arrays.equals() method is used to compare two arrays for equality. It returns true if both arrays have the same length ...
🌐
Coderanch
coderanch.com › t › 681183 › java › Array-equals
Array and .equals() vs == [Solved] (Java in General forum at Coderanch)
June 16, 2017 - Array classes, by default, do not override the equals() method. Instead these classes inherit the equals() method from the Object class, which does the comparison by reference (and not by content). Henry · Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
🌐
Educative
educative.io › answers › how-to-test-array-equality-in-java
How to test array equality in Java
The Arrays.equals method can be used to check if two arrays are equal. This method takes two arrays as arguments. import java.util.Arrays; class ArrayEquality { public static void main( String args[] ) { int [] arr1 = new int [] {20, 5, -95, ...
🌐
Baeldung
baeldung.com › home › java › java array › comparing arrays in java
Comparing Arrays in Java | Baeldung
January 8, 2024 - So far we only compared arrays based on their object identities. On the other hand, to check if two arrays are equal in terms of their contents, Java provides the Arrays.equals static method.
🌐
JetBrains
jetbrains.com › help › inspectopedia › ArrayEquality.html
Array comparison using '==', instead of 'Arrays.equals()' | Inspectopedia Documentation
March 31, 2026 - Reports operators == and != used to test for array equality. In most cases, testing for the equality of array contents is intended, which can be done with the java.util.Arrays.equals() method. A quick-fix is suggested to replace == with java.util.Arrays.equals().
🌐
Medium
medium.com › @ujjawalr › why-arrays-equals-and-give-different-results-and-how-to-compare-arrays-right-in-java-da7478742ca3
Why Arrays.equals() and == Give Different Results — And How to Compare Arrays Right in Java | by Ujjawal Rohra | Medium
July 29, 2025 - int[] arr1 = {1, 2, 3}; int[] arr2 = {1, 2, 3}; System.out.println(arr1 == arr2); // false System.out.println(Arrays.equals(arr1, arr2)); // true · You stare at the screen, scratch your head and go: ... Well, you’re not alone. This is one of those sneaky Java gotchas that trip up even experienced developers — especially when dealing with arrays.
🌐
SEI CERT
wiki.sei.cmu.edu › confluence › display › java › EXP02-J.+Do+not+use+the+Object.equals()+method+to+compare+two+arrays
EXP02-J. Do not use the Object.equals() method to compare two arrays - SEI CERT Oracle Coding Standard for Java - Confluence
In Java, arrays are objects and support object methods such as Object.equals(). However, arrays do not support any methods besides those provided by Object. Consequently, using Object.equals() on any array compares only array references, not their contents. Programmers who wish to compare the ...
🌐
QBasic on Your Computer
chortle.ccsu.edu › javaLessons › chap61 › ch61_24.html
Array.equals()
int[] arrayE = { 4, 3, 2, 1 }; int[] arrayF = { 1, 2, 3, 4 }; if ( Arrays.equals( arrayE, arrayF ) ) System.out.println( "Equal" ); else System.out.println( "NOT Equal" );