[Transfer] Don't be confused about the difference between == and equals in java anymore, this article guarantees that you can understand

Thinking that you should have seen this question countless times, or you may have read countless articles. If you are still confused or do not understand, then try to read this article, I believe you will be able to understand it.

1. Understand the meaning of "=="

In java, there are two main functions.

1. Basic data type: the comparison is whether their values ​​are equal, such as two variables of type int, the comparison is whether the values ​​of the variables are the same.

2. Reference data type: The comparison is whether the referenced addresses are the same. For example, if two User objects are created, the comparison is whether the addresses of the two Users are the same.

OK. Pay attention here, you will find that when I cited the example, I used the User object instead of String. Don't worry and read on.

Second, understand the meaning of equals

First look at his source code, the equals method exists in Object. Note that the source code here is equals in Object.

From this source code, you will find that the comparison is whether the reference of the current object is the same as the reference of obj, that is to say, the default of the comparison is the address. Remember above we used User instead of String? Here == compares the referenced addresses, and equals also compares the referenced addresses, so their effects are the same here.

Now you will find that the function of equals seems to be the same as that of ==, so what are those messy things of the String type? Read on to find out soon.

3. Rewrite equals

1. The equals method in String

Seeing this title, I believe you can already find the answer. There is no difference between == and equals in the Object object. In this way, the existence of the equals method is really meaningless, but then String rewrites equals on the basis of Object. , so the function is greatly changed. How to rewrite it? Let's find the answer in the source code of String:

From the above source code, the information we can get is: the equals method in String actually compares whether the contents of the strings are the same. That is to say, if you override equals classes like String and Date, you have to be careful. When used, it will be different from Object.

2. Test String

Take a look at the code below:

In the above code, three strings are defined, which are compared using == and equals respectively. Why is there such a result? It also needs to be explained from the perspective of memory.

3. Memory explanation

In java, we generally store objects in the heap area and put object references in the stack area. Therefore, the memory status of the above three strings should be as follows.

I understand now.

(1) String str1 = "Hello" will store a string object in the heap

(2)String str2 = new String("Hello") will store a string object again in the heap

(3)String str3 = str2 At this time, Str3 and Str2 are two different references, but point to the same object.

Take a look at the above comparison based on this picture:

(1) str1 == str2? Does that mean the address points to the same place? Obviously different.

(2) str1 == str3? Does that mean the address points to the same place? Obviously different.

(3) str2 == str3? Does that mean the address points to the same place? Obviously the content is the same, so it is true.

(4) str1. equals (str2)? Does that mean the address points to the same content? Same.

(4) str1. equals (str3)? Does that mean the address points to the same content? Same.

(4) str2. equals (str3)? Does that mean the address points to the same content? Same.

OK. I don't know if you can understand now?

4. Summary:

(1), comparison of basic types

Use == to compare values ​​for equality.

(2), reference type comparison

① Override the equals method, such as String.

The first case: use == to compare whether the String reference points to the same memory

The second case: use equals to compare whether the objects referenced by String are equal.

②The equals method is not rewritten, such as user-defined classes such as User

== and equals compare whether the reference points to the same block of memory.

5. A small problem

Of course, the String type is not over yet, and there is a small problem that needs everyone's attention. For example, look at the following code:

在这里多了一个intern方法。他的意思是检查字符串池里是否存在,如果存在了那就直接返回为true。因此在这里首先s1会在字符串池里面有一个,然后 s2.intern()一看池子里有了,就不再新建了,直接把s2指向它。

转载:https://baijiahao.baidu.com/s?id=1652442464525126879&wfr=spider&for=pc

Guess you like

Origin blog.csdn.net/m0_53121042/article/details/114847146