Java.lang.Class and equality

According to javadoc Class

Each array also refers to a class, which is reflected as an object class, which is shared by all arrays with the same element type and number of dimensions.

But when I run 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 following output

 1641745 1641745 false 

I am wondering why equals returns false, although both arrays are of type int and have the same size.

+6
source share
6 answers

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

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

You can also write:

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

As third-party hash codes, it is not necessary to specify equality, although this does not matter.

+8
source

try calling intArray.getClass().equals(int2Array.getClass())

+3
source

In general, a contract for a Java hash code only requires that:

  • Whenever it is called by the same object more than once during the execution of a Java application, the hashCode method must consistently return the same integer if the information used in equal comparisons with the object does not change. This integer should not remain consistent with one execution of the application on another execution of the same application.
  • If two objects are equal in accordance with the equals (Object) method, then calling the hashCode method for each of the two objects should lead to the same integer result.
  • It is not required that if two objects are unequal according to the equals method (java.lang.Object), then calling the hashCode method for each of the two objects must produce different integer results. However, the programmer should be aware that creating separate integer results for unequal objects can improve the performance of hash tables.

(From Java documentation on Object#hashCode )

Here you have two integer arrays that are not equal (for example, a.equals(b) => false ), but they are not (see the third point) needed to return unequal hash codes.


Also note that your code will work if you use Arrays.equals instead of Object#equals , as follows. Please note that Arrays.equals checks that "both arrays contain the same number of elements, and all the corresponding pairs of elements in 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.

+2
source

You are comparing two different arrays. Equality of an array is based on identity, not on the contents of the array. Since they are not the same array, the result will be false.

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

 out.println(Arrays.equals(intArray, int2Array); 
+1
source

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

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

When you compare two arrays using equals() , the default method is called.

0
source

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

Source: https://habr.com/ru/post/903618/


All Articles