= =:

Value comparison for primitive types, reference comparison for reference types.

/** * == */ @test public voidtestOne(){ int a = 200; int b = 200; Integer c = 200; Integer d = 200; System.out.println(a == b); // Compare with base type:trueSystem.out.println(c == d); //false
    }
Copy the code

equals:

Equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals Base types cannot use equals comparisons, but instead use ==, because base types do not have an equals method.

Take a look at Obeject’s rewrite of equals:

//Object:
public boolean equals(Object obj) {
        return(this == obj); } //Integer: // Check whether they are of the same typefalsePublic Boolean equals(Object obj) {public Boolean equals(Object obj) {if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false; } //String: // First compare the address, then check whether it is the same type, not directlyfalsePublic Boolean equals(Object anObject) {if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while(n-- ! = 0) {if(v1[i] ! = v2[i])return false;
                    i++;
                }
                return true; }}return false;
    }
Copy the code

The comparison of equals

class Cat{
    String name = "cat";
}
class Dog{
    String name = "dog"; } ________________________________________________________________________ euqals is * * * * / @ Test public voidtestTwo(){ int a = 200; int b = 200; Integer c = 300; Integer d = 300; Cat cat = new Cat(); Dog dog = new Dog(); System.out.println(c.equals(a)); //falseSystem.out.println(c.equals(d)); //true//System.out.println(a.equals(cat)); == == == == == == == == =Copy the code

hashcode():

Hashcode () is also an Object method. It is a native method implemented in C/C++, and Java calls the address value of the returned Object. But many classes in the JDK rewrite hashCode (). For example, Boolean means true and hash 1231, false and hash 1237,

	//Object:
	public native int hashCode(); //Integer Directly returns the value public inthashCode() {
        return Integer.hashCode(value);
    }
    public static int hashCode(int value) {
        returnvalue; } //String returns the hash code for this String. public inthashCode() {
        int h = hash;
        if (h == 0 && value.length > 0) {
            char val[] = value;

            for (int i = 0; i < value.length; i++) {
                h = 31 * h + val[i];
            }
            hash = h;
        }
        return h;
    }
Copy the code

Simple use of hashcode() :

class Cat{
    String name = "cat";
}
class Dog{
    String name = "dog";
}
---------------------------------------------------------------------
	@Test
    public void testThree(){ Cat cat = new Cat(); Dog dog = new Dog(); System.out.println(cat.hashCode()); //204349222 System.out.println(dog.hashCode()); //231685785 Integer a = 200; Integer b = 300; System.out.println(a.hashCode()); //200 System.out.println(b.hashCode()); / / 300}Copy the code

Equals vs. ==, hashcode():

  1. Two objects equal must have equal hashCode().
  2. Two objects whose hashCode() is equal are not necessarily equal in their equal().

Try to remember these concepts by association. Like the HashMap we used. Its structure is as follows:

Hashcode () equals, then they have the same bucket position, like Entry1 and Entry2. However, the equals of Entry1 and Entry2 do not necessarily want to be equal. Here is another example: Entry1= ABC, Entry2= ABC, then they are equal. If Entry1= ABC and Entry2=def, they are not equal. If Entry1= ABC and Entry2=def are equal, then they are on the same column