The equals method of Object in the API

The equals method of Object in the API


1. == is equal to operator
        A: It can compare basic types: the comparison is whether the values ​​are the same.

        B: Reference types can be compared: the comparison is whether the address values ​​are the same.

2、public boolean equals(Object obj)

        1) Indicates whether some other object is "equal" to this object. The equals method implements equality on non-null object references.
        2) Look at the source code: This method, by default, compares the address value.
                public boolean equals(Object obj) {
                    return (this == obj) ;
            }

        3) In general, comparing addresses is worth little. Therefore, this method needs to be rewritten: it is generally used to compare whether the values ​​of member variables of two objects of a class are equal.

        4) Override the equals( ) method in the Student class:

            @Override
            public boolean equals(Object obj) {
                // return super.equals(obj);
                // To improve here, you need to compare whether the values ​​of the object's member variables are the same! Therefore, the comparison here is name and age. But name is a string type, you cannot use == for comparison, you need to use the equal( ) method of the String class:

        /*         Method of string class: public boolean equals(Object anObject): Compare this string with the specified object, if and only if the parameter is not null and is a String object representing the same sequence of characters as this object, The result is true.

        */
        // (1) Improve the efficiency of the program:
        if(this == obj)
            return true;
        
        // (2) Improve the robustness of the program:
        // Let me first judge, if obj is an object of the Student class, you can ask Downcast to judge, if not, return false directly.
        // At this time, what we need to judge is whether the object is an object of a certain class?
        // Remember the format:     object name instanceof class name
        // Function: determine whether the object name is an object of the class name.

       // (3) The final version is best to use automatically generated.

        if(!(obj instanceof Student)) {
            return false;
        }
        
        Student ss = (Student)obj;
        return this.name.equals(ss.name) && this.age == ss.age;

        5) Example : ************************************************ ****************

package cn.itcast_03;

public class Student {
	private String name;
	private int age;

	public Student() {
		super();
	}

	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		//return super.equals(obj);
		//To be improved here, to compare whether the member variables of the object are the same!
		//The comparison here is name and age.
		//But name is a string type, you cannot use == for comparison, you need to use the equal method of the String class.
/* String class method: public boolean equals(Object anObject)
 * Compares this string to the specified object.
		if and only if the parameter is not null and is a String object representing the same sequence of characters as this object,
		The result is true.
		*/
		
/*		Student ss = (Student)obj;
		if(this.name.equals(ss.name) && this.age == ss.age) {
			return true;
		}else {
			return false;
		}*/
		
		// improve the efficiency of the program
		if(this == obj)
			return true;
		
		// Improve program robustness
		// Let me judge first, if obj is an object of the Student class, it can be judged by downcasting, if not, return false directly.
		// At this time, what we need to judge is whether the object is an object of a certain class?
		// remember the format: object name instanceof class name
		// Function: Determine whether the object name is an object of the class name.
		
		if(!(obj instanceof Student)) {
			return false;
		}
		
		Student ss = (Student)obj;
		return this.name.equals(ss.name) && this.age == ss.age;
	}
}
*************************************************
package cn.itcast_03;

/**
public boolean equals(Object obj) Compares this object with the specified object.
The result is true if and only if the parameter is not null and is an Integer object containing the same int value as this object.

	==
		A: Basic type: the comparison is whether the values ​​are the same
		B: Reference type: the comparison is whether the address value is the same


	public boolean equals(Object obj): Indicates whether some other object is "equal" to this object.
	The equals method implements equality on non-null object references.
	//Look at the source code: This method, by default, compares the address value.
	    public boolean equals(Object obj) {
        	return (this == obj);
    }
	 * 一般来说,意义也不大。所有,要重写该方法。一般是用来比较对象的成员是否相等。
	 * 
 * @author asus
 *
 */

public class StudentDemo {
	public static void main(String[] args) {
		Student s1 = new Student("qq",11);
		Student s2 = new Student("ss",22);
		System.out.println(s1 == s2);	//false
		Student s3 = s1;
		System.out.println(s1 == s3);	//true
		
		System.out.println("-----------------");
		System.out.println(s1.equals(s2));	//false
		System.out.println(s1.equals(s3));	//true
		
		System.out.println("-----------------");
		Student s4 = new Student("cmm",26);
		Student s5 = new Student("cmm2",26);
		Student s6 = new Student("cmm2",26);
		System.out.println(s4.equals(s5));	//false
		System.out.println(s5.equals(s6));	//true
		
		System.out.println("-----------------");
		Demo o = new Demo();
		System.err.println(s1.equals(o));
		
	}

}

class Demo {
	
}


2、






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324574024&siteId=291194637
Recommended