Differences and connections between == and the equals method

1、==

(1) The basic data types: whether the comparison value is equal to

   int a=1;
   int b=1;
   System.out.println(a==b);

Return true.

(2) reference data types, comparison is whether the same object:

        Student student1=new Student("201911","zhai");
        Student student2=new Student("201912","zhang");
        System.out.println(student1==student2);

Return false.

 

Two object reference point to a different object.

 

        Student student1=new Student("201911","zhai");
        Student student2=new Student("201912","zhang");
        System.out.println(student1==student2);

Return true.

 

 

 

Object references are different, but they point to the same object.

2, equals () method

(1) equals () method can not be applied to the basic data types of variables

(2) equals () methods applied to the reference data type, whether the comparison is the same object:

equals inherited from class Object to view source code:

   public boolean equals(Object obj) {
        return (this == obj);
    }

As can be seen, and its essence is to use == comparison data type is a reference to the same object.

(3) Some classes equals method has been rewritten (e.g.: String Class):

View source String class:

public 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;
    }

Comparison of the value of the object, whether the object is not the same object.

        String string1=new String("jiayou");
        String string2=new String("jiayou");
        System.out.println(string1.equals(string2));//true
        System.out.println(string1==string2);//false

Because were created two objects, object reference point to its own object, although the value of the two objects are equal, but the two objects are not the same object (the object of a different address).

(4) a custom equals method, the comparison object value are equal:

public Boolean the equals (Object obj) {
         // Analyzing current object transfer and, if the same object is directly 
        IF ( the this == obj) {// reference data type, whether the comparison is the same object as the
             return  to true ;
        }
        // whether the object is determined Vehicle type passed in is not the case directly Pass 
        IF (! {(The instanceof Vehicle obj))
             return  to false ;
        }
        // The obj downcast Vehicle reference, you can access Vehicle-specific content, or content can only access a total of 
        Vehicle v = (Vehicle) obj;
         return  the this .vType && == v.vType the this .vNum == v.vNum; // basic data types, comparing the value of the basic data types
    }

After the equals method of rewriting, the function of the equals method to compare two objects by the object becomes the same whether the object values ​​are equal.

 

Guess you like

Origin www.cnblogs.com/zhai1997/p/12398258.html