Explanation of the string equals () method

Source: Internet
Author: User

Let's take a look at the Code:

The output result of this Code is:

Ture

True

--------------

False

 

It seems that the object class is special. Let's take a look at the source code of the equals () method under the object.

The queals () method in an object is very simple. It simply determines whether the two references point to the same object. If yes, true is returned. If no, false is returned.

 

However, it is obviously incorrect to apply this method to the string object of the code above, so the equals () method must be rewritten in the string class. Let's take a look at its source code:

The source code of this method indicates that the equals () method of the string class works as follows:

1) determine whether two references point to the same object first. If yes, true is returned directly.

2) Determine whether the real parameter is a string instance. If not, false is returned directly.

3) if the real parameter is an instance of string, compare the strings passed in with the current character one by one to see if they are consistent.

 

Summary:

A) equals () method: This method is defined in the object class. Therefore, every class in Java has this method. For the equals () method of the object class, it determines whether the caller and the passed reference point to the same object. Therefore, for object classes, the equals () method is equivalent to =.

 

B) For the string class, the equals () method determines whether the content of the current string is consistent with that of the passed string.

Explanation of the string equals () method

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.