Java Learning: The difference between string comparison "=" and "equals" and C #

Source: Internet
Author: User

.. Net, its string-Specific Resident mechanism ensures that there is only one instance of strings with the same character sequence in the same process, which can avoid repeated instantiation of strings with the same content, to reduce performance overhead.

Let's reviewCode:

View code

 1           Public   Static   Void  Teststring ()  2   {  3 String S = "  ABC  "  ; 4 String S1 = "  ABC  "  ;  5 String S2 = "  ABC  "  ;  6   7   8 Console. writeline ( "  S1 = S2?  " + (S1 = S2 )); //  True  9 Console. writeline ( "  S1.equals (S2 )?  " + S1.equals (S2 )); //  True  10 Console. writeline ( "  String. Compare (S1, S, true )?  " + String. Compare (S1, S, True )); // 0  11 Console. writeline ( "  ------------------------  "  );  12   13   14               Char [] CHR = { '  A  ' , '  B  ' ,'  C  '  };  15 String S3 = New  String (CHR );  16 Console. writeline ( "  S1 = S3?  " + (S1 = S3 )); //  True  17 Console. writeline ( " S1.equals (S3 )?  " + S1.equals (S3 )); //  True  18 Console. writeline ( "  String. Compare (S3, S, true )?  " + String. Compare (S3, S, True )); //  0  19 Console. writeline ( "  ------------------------ "  );  20   21 String T = "  BC  "  ;  22 String S4 = "  A  " + T;  23 Console. writeline ( "  S1 = S4? " + (S1 = S4 )); //  True  24 Console. writeline ( "  S1.equals (S4 )?  " + S1.equals (S4 )); //  True  25 Console. writeline ( "  String. Compare (S4, S, true )?  " + String. Compare (S4, S, True ));//  0  26 Console. writeline ( "  ------------------------  "  );  27   28 String S5 = "  A  " + "  BC  "  ; 29 Console. writeline ( "  S1 = S5?  " + (S1 = S5 )); //  True  30 Console. writeline ( "  S1.equals (S5 )?  " + S1.equals (S5 )); //  True  31 Console. writeline ( " String. Compare (S5, S, true )?  " + String. Compare (S5, S, True )); //  0  32   33   34   Console. Read ();  35   36   37 }

From the running results, we can see that no matter how difficult you are, as long as the content of the two strings is identical, there is always only one reference.

 

Java also has a similar mechanism called "String constant pool",Java allows you to create multiple instances with the same content using the new string (string Str) method.. To distinguish between the two cases, the = and equals in Java give different meanings when they are used to determine whether the strings are equal.

= Used to determine whether two strings are usedReferenceSame, while equals is used to determine whether two strings areContentSame

View code

 1   Public   Static   Void  Teststring (){  2 String S = "ABC" ;  3 String S1 = "ABC" ; 4 String S2 = "ABC" ;  5           6 System. Out. println ("S1 = S2? "+ (S1 = S2 )); //  True  7 System. Out. println ("s1.equals (S2 )? "+ S1.equals (S2 )); //  True  8 System. Out. println ("s1.20.signorecase (s )? "+ S1.equalsignorecase (s )); //  True  9 System. Out. println ("------------------------" );  10           11 String S3 = New String ("ABC" );  12 System. Out. println ("S1 = S3? "+ (S1 = S3 )); //  False  13 System. Out. println ("s1.equals (S3 )? "+ S1.equals (S3 )); //  True  14 System. Out. println ("s3.20.signorecase (s )? "+ S3.equalsignorecase (s )); //  True  15 System. Out. println ("------------------------" );  16           17           Char [] CHR = {'A', 'B', 'C' };  18 String S4 = New  String (CHR );  19 System. Out. println ("S1 = S4? "+ (S1 = S4 ));//  False  20 System. Out. println ("s1.equals (S4 )? "+ S1.equals (S4 )); //  True  21 System. Out. println ("s4.20.signorecase (s )? "+ S4.equalsignorecase (s )); //  True  22 System. Out. println ("------------------------" );  23           24 String T = "BC" ; 25 String S5 = "A" + T;  26 System. Out. println ("S1 = S5? "+ (S1 = S5 )); //  False  27 System. Out. println ("s1.equals (S5 )? "+ S1.equals (S5 )); //  True  28 System. Out. println ("s5.20.signorecase (s )? "+ S5.equalsignorecase (s )); //  True  29 System. Out. println ("------------------------");  30           31 String S6 = "A" + "BC" ;  32 System. Out. println ("S1 = S6? "+ (S1 = s6 )); //  True  33 System. Out. println ("s1.equals (s6 )? "+ S1.equals (s6 )); //  True  34 System. Out. println ("s6.equalsignorecase (s )? "+ S6.equalsignorecase (s )); //  True 35           36           37 }

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.