简体   繁体   中英

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.

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.

尝试调用intArray.getClass().equals(int2Array.getClass())

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 (eg 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.

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);

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.

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM