Java FAQ Java Strings compare those things

Source: Internet
Author: User


The following code:

public class example013 {public static void  main (String[] args)  {final String pig =  "Length:8";final string  pig1 =  "Length:8"; Int length = pig.length ();final string dog =  " Length: " + pig.length (); System.out.println ("Pig ' s length is :"  + length); System.out.println ("pig is same as dog ? "  + pig == dog);  //output 1system.out.println ("pig is same as pig1 ? "  +  (pig ==  PIG1));//Output 2system.out.println ("pig is same as dog ? "  +  (pig  == dog));//  output 3system.out.println ("pig is same as dog ? "  +   (Pig.equals (dog)));//  output 4}} 


Output Result:

Pig ' s length is:8falsepig is same as PIG1? Truepig is same as dog? Falsepig is same as dog? True

Results Analysis:

Learn about equals and = = The difference between the small partners may feel today this topic is very simple, do not know the small partners quickly go to the bad. Word, equals is more strict than = =, two objects if = =, must equals, but two objects equals, may not necessarily = =. You might know that the compile-time constants for String types are memory-qualified . In other words, any two constant expressions of type string, if they are marked with the same sequence of characters, are represented by the same object reference . So the output 2 printing true is also very flattering. Look at output 3 again, but the print is false. Let us know here:

If you can, your code should not rely on the memory-qualification mechanism of string constants. The memory-qualification mechanism is designed to reduce the memory footprint of virtual machines, and is not designed as a tool that programmers can use.

Even worse, if your code relies on the memory-qualification mechanism for correctness, then you have to carefully understand which fields and parameters must be memory-qualified. The compiler will not help you to check these invariants, because the memory-qualified and unqualified strings are represented by the same type (string). The bugs that are caused by failing to qualify strings in memory are very difficult to detect.

Therefore, when using string comparisons, it is best to use the Equals method, such as output 4, so that the resulting results can be guaranteed to be controllable and accurate.


After analyzing the output 2, 3, 4, and then look at the output 1, its output is just a pure false, and there is no leading "pig is same as dog". What is this for? The reason is that the + priority is higher than = =, so the code actually executed here is as follows:

System.out.println ("Pig is same as dog" + pig) = = dog);

Without suspense, the result is of course false.



via blog or public message explore together. )

Source code Address: Https://github.com/rocwinger/java-disabuse


This article from "Winger" blog, declined reprint!

Java FAQ Java Strings compare those things

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.