Aravind Yarram
Aravind Yarram

Reputation: 80194

java.lang.Class and equality

According the javadoc of Class

Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions.

But when I run the below

int[] intArray = { 1, 2 };
out.println(intArray.getClass().hashCode());
int[] int2Array = { 1, 2 };
out.println(int2Array.getClass().hashCode());

out.println(intArray.equals(int2Array));

I get the below output

1641745
1641745
false

I am wondering why the equals is returning false even though both the arrays are of int type and have the same dimensions.

Upvotes: 6

Views: 2549

Answers (6)

Paul Bellora
Paul Bellora

Reputation: 55223

This is because you're calling equals() on the array instances themselves instead of their Class object. Try:

out.println(intArray.getClass().equals(int2Array.getClass())); //prints true

You could also write:

out.println(int[].class.equals(int[].class)); //prints true thankfully

As an aside, matching hash codes don't necessarily indicate equality, though that doesn't matter here.

Upvotes: 8

Jon Newmuis
Jon Newmuis

Reputation: 26522

In general, the Java hash code contract only requires that:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

(From the Java documentation on Object#hashCode)

Here you have two integer arrays that are not equal (e.g. a.equals(b) => false), but they are not (see the third point) required to return unequal hashcodes.


Also, note that your code will work if you use Arrays.equals instead of Object#equals, as follows. Note that Arrays.equals checks that "both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal."

int[] intArray = { 1, 2 };
out.println(intArray.getClass().hashCode());
int[] int2Array = { 1, 2 };
out.println(int2Array.getClass().hashCode());

out.println(Arrays.equals(intArray, int2Array));

See http://www.ideone.com/HaysD for a working example.

Upvotes: 2

DwB
DwB

Reputation: 38320

Run this and the answer will be clear:

int[] array1 = { 1, 2 };
int[] array2 = { 1, 2 };

System.out.println("array1.hashcode: " + array1.hashCode());
System.out.println("array2.hashcode: " + array2.hashCode());
System.out.println("array1.class.hashcode: " + array1.getClass().hashCode());
System.out.println("array2.class.hashcode: " + array2.getClass().hashCode());

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234847

The default implementation of equals() is defined by Object.equals(Object):

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

When you compare two arrays using equals(), theis default method is invoked.

Upvotes: 0

erickson
erickson

Reputation: 269797

You are comparing two different arrays. Array equality is based on identity, not array content. Since they are not the same array, the result is false.

If you want to test the content of two arrays for equality, there are helper methods in java.util.Arrays.

out.println(Arrays.equals(intArray, int2Array);

Upvotes: 1

Jan Vorcak
Jan Vorcak

Reputation: 20019

try to call intArray.getClass().equals(int2Array.getClass())

Upvotes: 3

Related Questions