46

Why is the following code printing "Different."?

boolean[][] a = { {false,true}, {true,false} };
boolean[][] b = { {false,true}, {true,false} };

if (Arrays.equals(a, b) || a == b)
    System.out.println("Equal.");
else
    System.out.println("Different.");
user905686
  • 4,491
  • 8
  • 39
  • 60

4 Answers4

78

Why is the following code printing "Different."?

Because Arrays.equals performs a shallow comparison. Since arrays inherit their equals-method from Object, an identity comparison will be performed for the inner arrays, which will fail, since a and b do not refer to the same arrays.

If you change to Arrays.deepEquals it will print "Equal." as expected.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Documentation says to Array.equals: "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." So doesnt that mean a complete comparison...? – user905686 Nov 08 '11 at 13:28
  • 1
    No, because the elements of `a` and `b` don't `equals(...)` each other. Try it out yourself: `a[0].equals(b[0])` and you'll see that it's false. – aioobe Nov 08 '11 at 13:33
  • It would be better to check for `a == b` before calling `deepEquals`, wouldn't it? – Victor Sorokin Nov 08 '11 at 13:36
  • 2
    No, it would be better to leave that redundancy out all together. (The standard implementation of `Arrays.equals` start with that check anyway.) – aioobe Nov 08 '11 at 13:38
  • 1
    Note that Arrays.equals is adequate for single dimension arrays: in this example, Arrays.equals(a[0], b[0]) is true. – Stephen Schaub Jun 02 '16 at 15:06
17

It's really not obvious.

First of all, the == operator just compare two pointers. Because a and b are distinct objects located at different memory addresses, a == b will return false (Hey, Java purists, I know that the == actually compare object identities. I'm just trying to be didactic).

Now let's take a look at the equals() implementation of arrays:

boolean[] c = new boolean[] { false, true, false };
boolean[] d = new boolean[] { false, true, false };

if (c.equals(d)) {
    System.out.println("Equals");
} else {
    System.out.println("Not equals");
}

That would print Not equals because no array instance actually implements the equals() method. So, when we call <somearray>.equals(<otherarray>) we are actually calling the Object.equals() method, which just compare two pointers.

That said, notice that your code is actually doing this:

boolean[] a0 = new boolean[] { false, true };
boolean[] a1 = new boolean[] { true, false };
boolean[] b0 = new boolean[] { false, true };
boolean[] b1 = new boolean[] { true, false };
boolean[][] a = new boolean[][] { a0, a1 };
boolean[][] b = new boolean[][] { b0, b1 };

if (Arrays.equals(a, b) || a == b)
    System.out.println("Equal.");
else
    System.out.println("Different.");

The Arrays.equals(a, b) will eventually call a0.equals(b0) which will return false. For this reason, Arrays.equals(a, b) will return false as well.

So your code will print Different. and we conclude that Java equality can be tricky sometimes.

fernacolo
  • 7,012
  • 5
  • 40
  • 61
6

Use Arrays.deepEquals() for multidimensional arrays.

MOleYArd
  • 1,258
  • 1
  • 12
  • 16
-1
public static boolean equal(double[][] a, double[][] b) {
        if (a == null) {
            return (b == null);
        }
        if (b == null) {
            return false;  // already know 'a' isn't null
        }
        if (a.length != b.length) {
            return false;
        }
        for (int i = 0; i < a.length; i++) {
            if (!Arrays.equals(a[i], b[i])) {
                return false;
            }
        }
        return true;
    }
Ashok Domadiya
  • 1,124
  • 13
  • 31
  • 6
    It´s not good practice to reimplement [`Arrays.deepEquals`](http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html#deepEquals%28java.lang.Object[],%20java.lang.Object[]%29). The answers given so far cover good explanations and solutions. – user905686 Jul 10 '12 at 11:58