What is the difference between equals and = ?, Equals differences

Source: Internet
Author: User

What is the difference between equals and = ?, Equals differences
Equals and =

I, a beginner in Java, has been annoyed by the equals () method for a long time. I cannot figure out when to compare the address and when to compare the content? Because you want to find a job, you have to interview. I read a lot of blogs from my predecessors and recorded many notes. I remember what I compared under the Object is the address, and what I compared under the String class is the value. However, when we encounter StringBuffer, we don't know what the comparison is? Therefore, you may have to worry about the source code and uncover the fog.

1. First, we need to know that the equals () method is the method defined under the Object class, but all java classes are indirectly or directly inherited from the Object class. Therefore, according to the inheritance principle of java, all classes have the equals () method. Therefore, it is especially important for us to determine whether the equals () method has been rewritten by developers or even JDK writers.

Let's take a look at the equals () method under the Object class.

We can see all the methods in the Object class through Outline. Click the equals () method to see the source code.

Therefore, the results are clear at a glance.

2. Check the equals () method of the String class.

There is an instanceof operator for type check (whether it belongs to the String class or whether it is a derived class of the String class). if not, exit the if statement and return false. Otherwise, convert the input Object to the String type, and then use a char array. Use the while loop to compare whether each character is consistent. If they are consistent, true is returned. Otherwise, false is returned.

3. Check the packaging class, such as Integer.

However, the caching mechanism is another thing to note for the basic type packaging class,

If the input value is a direct number (-128,127), whether it is an int or an Integer, it will be automatically wrapped after JDK 5. Therefore, all data is cached in IntegerCache. cache and the same object is returned.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.