Confusing knowledge of Java == and the difference between equals () method

I often encounter this problem in Java learning and interviews. Although thinking of simplicity, I decided to record it. After all, the food...

1.“==”

== is the heap memory address of the two objects that are directly compared . If they are equal, it means that the two references actually point to the same object address. But we often encounter such a problem
Insert picture description here
. The results of the operation:
Insert picture description here
From the results, it can be seen that since == is a comparison address, then what is the address of the int data and what is the String?

For the ** basic data types (byte, short, char, int, float, double, long, boolean) **, they are stored as constants in the constant pool in the method area with the HashSet strategy. For such The string "123" is the same thing,In the constant pool, a constant will only correspond to one address, So no matter how many 123, "123" data will only store one address, so their references are all pointing to the same block address, so basic data types and String constants can be directly compared directly through == of.

In addition, for the basic data packaging types (Byte, Short, Character, Integer, Float, Double, Long, Boolean) in addition to Float and Double , the other six are implemented constant pools, so for these data types In other words, generally we can also directly pass == to judge whether they are equal. Then there is another question to test everyone ↓
Insert picture description here
Guess what the result is?
————————————————————————————————————The
result is true and false. Unexpectedly! In fact, because the storage range of Integer in the constant pool is [-128,127], 127 is in this range, so it is stored directly in the constant pool, and 128 is not in this range, so a new object will be created in the heap memory To save this value, m and n respectively point to two different object addresses, which leads to inequality.

2, equals () method

Many partners say that equals compares the content of the object, which is not accurate.

First, let's take a look at the equals method defined in the Object class [source code] ↓
Insert picture description here
You can see the source code. The equals method of the Object type is directly compared with ==, and there is no difference between ==.

So why say the difference between equlas and ==? Because the equals method can be rewritten by ourselves.

As we all know, all our classes directly or indirectly inherit from the java.lang.Object class, so we can achieve the comparison method we want by overriding the equals method. Let's take a look at the equals method that the eclipse compiler automatically generates for us↓

(1) Handwritten equals
Insert picture description here
(2) The system automatically generates equals (shortcut key: Shift+Alt+S)
Insert picture description here

It can be seen that the eclipse compiler is still very smart. It first judges whether the addresses of the two objects are equal, and if they are not equal, perform the following member variable judgments.

But this method body can be implemented by ourselves, even if we directly return true, it doesn't matter how we write it as long as it can meet our business needs. Therefore, what equals compares is not necessarily the content of the object, it can also be compared by other information.

3. Summary

  1. The function of "==" is to judge whether the addresses of two objects are equal. That is to determine whether the two objects are the same object. (The basic data type == compares the value, and the reference data type == indicates the memory address)
  2. equals(): Its role is to judge whether two objects are equal. But it generally has two use cases:
    (Note: The equals() method cannot be applied to variables of basic types, but the packaging classes corresponding to the basic types are possible)
    (1) The equals() method is not covered (overridden) by the class. When comparing two objects of this class through equals(), it is equivalent to comparing the two objects through "==". (For details, see the source code above) [ie: compare the memory address of the object pointed to by the variable of the reference type ]
    (2) The class covers the equals() method. Generally, we rewrite this method to compare two objects Whether the contents are equal, if their contents are equal, return true (that is, consider the two objects equal). [That is: the content of the pointed object is compared (such as String, Date, etc.) ]

Therefore, the "==" and equals() methods do not have many knowledge points, but they are really easy to confuse. I personally recommend combining the code to understand the knowledge points, distinguish the two clearly, and rewrite the equals() method flexibly .

Guess you like

Origin blog.csdn.net/weixin_46312449/article/details/112726453