Interview questions—————— The difference between equals and ==

Interviewer: Tell me about the difference between equals and ==?

01. For == :

If you are comparing basic data type variables, compare the values ​​of the two variables to see if they are equal. (Not necessarily the same data type)
If the comparison is a reference data type variable, compare whether the address values ​​of the two objects are the same, that is, whether the two references point to the same object entity.

Test basic data types:

		int a = 20;
        int a = 20;
        double c = 20.0;
        char i = 20;
        char j = 'A';
        boolean boo = true;
        System.out.println(c==a);//true
        System.out.println(a==i);//true
        System.out.println(boo==a);//编译不通过

Among them, the Boolean type cannot participate in the operator == comparison,

Report the following error:
Operator'==' cannot be applied to'boolean','int'

Test reference data type:

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

    public Student(int age, String name) {
    
    
        this.age = age;
        this.name = name;
    }
    public static void main(String[] args) {
    
    
        Student stu1 = new Student(11, "张三");
        Student stu2 = new Student(11,"张三");
        System.out.println(stu1==stu2);
    }
}

Analysis:
Although the attribute values ​​of the two objects are equal.
But the system will allocate a memory address value for each new object. (Stored in the heap)
The references of the two objects point to different address values, so the result is false.

Let me test you:
This code is very familiar, why is the result true?

	    String str = new String("hello");
        String str2 = new String("hello");
        System.out.println(str.equals(str2));//true

This is because the String class rewrites the equals method. The source code is as follows:

public boolean equals(Object anObject) {
    
    
		//判断调用equals方法的对象和 形参引用地址是否相等
        if (this == anObject) {
    
    
            return true;
        }
        //判断左边形参引用是否是右边String类的实例对象
        if (anObject instanceof String) {
    
    
        	//将形参引用强制转换为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;
    }

Maybe you have questions again, why is the following code true?

String str = "hello";
String str2 = "hello";
System.out.println(str==str2);//true

Analysis: When
str2 creates an object, it is found that "hello" already exists in the constant pool. At this time, there is no need to create a new object to directly point the str2 reference to "hello", so that the str and str2 references point to the same address value, so the result is true.

Knowing the above point, summarize the usage of equals.

02. For equals :

If the equals method is overridden in the class, compare whether the content is equal.
String, Date, File, and wrapper classes all rewrite the equals method of the Object class.

If the equals method is not overridden in the class, compare whether the address values ​​are equal (whether they point to the same object entity).

	    Student stu1 = new Student(11, "张三");
        Student stu2 = new Student(11,"张三");
        System.out.println(stu1.equals(stu2));//false

Since equals compares whether the content is the same, why is the result still false?

Recalling knowledge:
In Java, we know that the superclass of any class is the Object class, and the Student class also inherits the Object class.

Viewing the equals method in the Object class is also == comparison (that is, comparing address values), so the result is of course false.

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

In this case, how do we ensure that the contents of the two objects are the same?
Here we need to rewrite the equals method?

  @Override
    public boolean equals(Object obj){
    
    
        if (this == obj){
    
    
            return true;
        }
        if (obj instanceof Student) {
    
    
            Student stu = (Student)obj;
            return this.age == stu.age && this.name.equals(stu.name);
        }
        return false;
    }

test:

true

If it is helpful to you, help me Sanlian~~~!

Guess you like

Origin blog.csdn.net/lirui1212/article/details/115363917