Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: How to test on array equality?

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."); 
like image 319
user905686 Avatar asked Nov 08 '11 13:11

user905686


People also ask

How do you check if an array is equal to another Java?

The Arrays. equals() method checks the equality of the two arrays in terms of size, data, and order of elements. This method will accept the two arrays which need to be compared, and it returns the boolean result true if both the arrays are equal and false if the arrays are not equal.

Can we use == to compare arrays in Java?

Java provides a direct method Arrays. equals() to compare two arrays. Actually, there is a list of equals() methods in the Arrays class for different primitive types (int, char, ..etc) and one for Object type (which is the base of all classes in Java).

How do you check if two arrays are equal?

Check if two arrays are equal or not using Sorting Follow the steps below to solve the problem using this approach: Sort both the arrays. Then linearly compare elements of both the arrays. If all are equal then return true, else return false.


2 Answers

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.

like image 98
aioobe Avatar answered Sep 20 '22 21:09

aioobe


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.

like image 29
fernacolo Avatar answered Sep 18 '22 21:09

fernacolo