Two ways to compare string from Java source

Source: Internet
Author: User

String comparison of two methods

= = and Equals method

==:

= = Compares the address of a string in memory

code example:

1  Public classEqualsdemo {2 3     /**4      * @paramargs5      */6      Public Static voidMain (string[] args) {7string S1 = "string";8String s2 = "string";9String s3 =NewString ("string");TenString S4 =NewString ("string"); OneSystem.out.println (S1 = =S2); ASystem.out.println (S1 = =S3); -System.out.println (S3 = =S4); -     } the}
= = String comparison code example

Operation Result:

True

False

False

Results Analysis:

When the S1 is created, the program finds the string "string" in the string buffer pool, creates a "string" string in the string buffer pool, and when the S2 is created, the program finds the "string" string in the string buffer pool to find the same value. Referring directly to the value of "string" in S1, the comparison is actually the address of two strings in the string buffer pool. When you create a S3, it is not put "string" into the string buffer pool, because it is created an object, Instead of a constant string. A new object is also created when the S4 is created. So, in fact, S1 and S2 point to the address is a string constant pool of a "string" constant, and S3 and S4 are different objects, so s1==s2 is true, S1==s3 is False, S3==s4 is False

Equals () Method:

The Equals method compares the values in a string

code example:

1  Public classEqualsdemo {2 3     /**4      * @paramargs5      */6      Public Static voidMain (string[] args) {7string S1 = "string";8String s2 = "string";9String s3 =NewString ("string");TenString S4 =NewString ("string"); One System.out.println (s1.equals (S2)); A System.out.println (S1.equals (S3)); - System.out.println (S3.equals (S1)); - System.out.println (S3.equals (S4)); the     } -}
Equals method code example

Operation Result:

True

True

True

True

Results Analysis:

The Equals method is overridden in the string class in Java

1   /**2 * Compares this string to the specified object. The result is {@code3 * true} if and only if the argument are not {@codenull} and is a {@code4 * String} object that represents the same sequence of characters as this5 * object.6      *7      * @paramAnObject8 * The object to compare this {@codeString} against9      *Ten      * @return  {@codetrue} If the given object represents a {@codeString} One * Equivalent to this string, {@codefalse} otherwise A      * -      * @see#compareTo (String) -      * @see#equalsIgnoreCase (String) the      */ -      Public Booleanequals (Object anobject) { -         if( This==anobject) { -             return true; +         } -         if(AnObjectinstanceofString) { +String anotherstring =(String) anobject; A             intn =value.length; at             if(n = =anotherString.value.length) { -                 CharV1[] =value; -                 CharV2[] =Anotherstring.value; -                 inti = 0; -                  while(n--! = 0) { -                     if(V1[i]! =V2[i]) in                         return false; -i++; to                 } +                 return true; -             } the         } *         return false; $}
the Equals method in the String class

From the source we can analyze: The Equals method first compares the object reference, if the address of two objects is the same, then directly returns True, if the object is compared to a string type, or a subclass of string type, then the value comparison of the String class ( The method of comparison includes the length of the two characters, if the length is not the same directly return false, if the same, and then the characters are compared, if all the characters are the same return true, simply say is worth comparison)

Two ways to compare string from Java source

Related Article

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.