0

I have run this small program:

String[] a = {"a","b"};
String[] b = {"a","b"};


    if (a.equals(b)){
        System.out.println("woop");
    }else{
        System.out.println("doh!");
    }


    if (Arrays.equals(a, b)){
        System.out.println("woop");
    }else{
        System.out.println("doh!");
    }

The output of the program is "doh! woop".

I get the difference between the == and .equals but what is the difference between these equals operations?

Daniel Cohen
  • 301
  • 1
  • 4
  • 14

4 Answers4

3

TL;DR

equals compares array instances, Arrays.equals compares array elements.

Full Explanation

Arrays do not implement equals. As a result, they hand the method off to Object which is implemented as follows.

public boolean equals(Object obj) {
   return (this == obj);
}

Thus, equals on an array is the same as ==.

Arrays.equals has the following description. It compares the elements of the array.

Returns true if the two specified arrays of longs are equal to one another. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null.

Dan Grahn
  • 9,044
  • 4
  • 37
  • 74
2

Arrays.equals(Object[] a, Object[] a2)...

returns true if the two specified arrays of Objects are equal to one another. The two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. Two objects e1 and e2 are considered equal if (e1==null ? e2==null : e1.equals(e2)). In other words, the two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null.

In short, all your Strings are compared through equals.

See API.

Object.equals, which is invoked when comparing two arrays with the array1.equals(array2) idiom, compares the reference, just like ==.

See source for java.lang.Object:

public boolean equals (Object o) {
    return this == o;
}
Mena
  • 47,782
  • 11
  • 87
  • 106
1

The difference is that a.equals(b) will use == under the hood, since arrays don't override the Object.equals() method. You use this to test for instance equality between arrays.

So in your case, a.equals(b) will return false, since the two arrays are different instances.

Instead, Arrays.equals() actually compares what's inside the arrays. If the arrays contain the same values, then Arrays.equals() returns true. You use this to test for semantic equality between arrays.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
1

With arrays, a.equals(b) is the same as a == b. This is checking if the two array instances are the same. Arrays.equals(a,b) takes the time to compare each element of both arrays and checks if the elements are equal, which is different than checking if the containers themselves are equal.

A quick analogy: Let's say a and b are buckets. Asking if the two buckets are the same is not the same as asking if what is in the buckets is the same.

MadConan
  • 3,749
  • 1
  • 16
  • 27