当前位置:网站首页>What does the equals method compare? Who told you

What does the equals method compare? Who told you

2022-06-26 16:57:00 Tianweichang

Official account “Java Back end technology stack ”

reply “000” Get the necessary e-books for programmers

The story

Some time ago , When I gave an online mock interview to a little friend , A casual question :” say something == and equals Differences in methods ?“, The result gave me an answer equals The method is to compare the contents .

Let's talk about it first ==,== There are two cases of comparison :

1. Compare the basic data types

int a=100;
int b =10;
// Compare the values of two variables 
if(a==b){
    System.out.println(" Something is wrong. ");
}

2. The comparison is the reference type

Integer a=Integer.valueOf(100);
Integer b =new Integer(100); 
// The comparison is a and b Whether the addresses of the objects pointed to are the same 
if(a== b){
    System.out.println(" Something is wrong. ");
}

then , Let's talk about equals Method .

equals The method is in Object As defined in , As follows :

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

From this method , We can conclude that : If you do not override equals Method , Then the two object addresses are compared .

as for , Rewrote , What is the comparison ?

It's like eating , In most people's minds , When it comes to eating , You will think of eating noodles or rice . I'm sorry , Many people now eat only vegetables , No rice or noodles .

So let's talk about that , Two common classes :String Classes and Integer class .

String Class equals Method :

public boolean equals(Object anObject) {
    // First compare the address of the object 
    // The object address is the same , That proves to be the same object 
    if (this == anObject) {
         return true;
    }
    // Comparison of type 
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = value.length;
        // Compare the length 
        if (n == anotherString.value.length) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = 0;
            while (n-- != 0) {
                // Compare content 
                if (v1[i] != v2[i])
                     return false;
                i++;
            }
            return true;
        }
    }
   return false;
}

From the source code logic of the whole method , It's easy for us to see ,String Class equals The method does not have to compare the content , First, compare the addresses of two objects , Then compare the length , In comparison with the specific content .

Integer Class equals Method :

public boolean equals(Object obj) {
    // Comparison of type 
    if (obj instanceof Integer) {
        // Unpack first   Then compare the specific values 
        // Be careful   there value  yes int Variable of type 
        return value == ((Integer)obj).intValue();
    }
    return false;
}

summary

This interview question , It seems very simple , I can also ask two other friends who have worked for three years , The answer is the same as that of the previous friend . Here is a summary of this :

== If it is used for the comparison of basic data classes , Then the specific value is compared , If you are comparing reference types ( Packaging type ), The comparison is whether the object addresses pointed to by the two objects are equal .

equals Method , The default is to compare whether the addresses of two objects are the same . If you are right equals Method overridden , It depends on how it is rewritten .

Okay , So much for today , Remember to follow me , Tell me something different , It can also take you to see some source code , remember : Many good things can only be seen in the source code .

Recommended reading

原网站

版权声明
本文为[Tianweichang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170500468874.html