JAVA string comparison: Equals () and = =

Source: Internet
Author: User

About string comparisons with equals or = =, summarize.

The string is an object: 1. " = = "All" object variables are compared by = = Two objects are referenced in the heap. 2. " Equals () "The Equals method of all objects inherits from the method of the object class, and string overrides the comparison algorithm of the Equals method, which compares the contents of the string, that is, the sequence of the characters in the heap 3. Characteristic string is a special kind of object, The special point here is that during the run, its assignment directly changes the reference (of course, any object assignment is a direct change of reference, but the string is often easily understood as it is operating on the original basis, especially the + = operation), rather than in the original referenced memory block to do the modification. (hence the advent of the StringBuffer Class) Conclusion: 1.String S1 = "Hello"; String s2 = "Hello"; System.out.println (s1.equals (S2)); //truesystem.out.println (S1 = s2);      / /true according to the above, the first true is not difficult to understand. The 2nd reason is also true because the Java compiler is similar to the "text pool mechanism" in C, that is, all the string constant declarations in the source code are placed in a block of memory area, So when a string declaration is assigned a string constant, then if there is a string constant in the literal pool, the compiler will assign its reference directly to the variable. Therefore, when using = = to compare S1 and S2, you will get true.2.string S1 = "Hello"; String s2 = "Hello"; s1 = S1 + "a"; s2 = s2 + "a"; System.out.println (s1.equals (S2)); //truesystem.out.println (S1 = s2);      / /false 3rd, the assignment operation of Line 4 executes during run time, according to the above Knowledge point 3: Two variable references will be changed. Therefore, when you compare the = =, you will get false; The string content obtained during the run will not be placed in the so-called "text Pool". Then if a variable with the same content refers to the same content, it needs to check its contents each time the string changes, and then look in the text pool for constants that have the same content, too wasteful. (the "text pooling mechanism" may also be why a string object changes directly to the referencedOne reason, because if you directly manipulate a referenced block of memory, another string variable that points to the same reference will also change, with logic confusion. 3. Code slightly. When a string object is read from a different place (string constant; a local resource file reads; Network transport: such as form submission; etc.) when obtained: Except that the reference to the string variable that was assigned with the same constant during compilation is the same, all others are new references. Therefore, when compared to this variable directly with = = will get false, even if the content of the same application conclusion: 1. If it is a simple comparison of string content: Equals () 2. If you really want to compare the references of two variables: use = =

JAVA string comparison: Equals () 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.