Java: How to test on array equality?

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.

Leave a Comment