The Equals method of string and = =

Source: Internet
Author: User

The object of the string class is a string constant, and everything seems to change the operation of a string object, but it only changes the string referenced by the string reference variable.

The strings in Java are stored in a common storage pool where references point to the corresponding location in the storage pool, where the compiler can let the string be shared, that is, two different references that can point to the same string constant, but the physical storage of the string is actually only one copy.

To determine whether two strings are equal, be sure to use the Equals method. "= =" can only determine whether two references point to the same string, and the same string may be stored in different physical storage locations.

In fact, only string constants are shared, and the results of operations such as + or substring are not shared. (Not absolute)

1 String s= "People"; 2 String t= "People"; 3 char c[]={' P ', ' e ', ' o ', ' P ', ' l ', ' E '};  4 System.out.println (s== "PE" + "O" + "ple");    True5System.out.println (T.equals (c));  False6 System.out.println (t.equals (new String ("people")); True

In summary, do not use "= =" to determine the equality of strings in order to avoid errors.

The Equals method of string and = =

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.