Home > Back-end >  About the string comparison, why different results?
About the string comparison, why different results?

Time:09-25

String Java=new StringBuilder (" ja "), append (" va "). The toString ();
String good=new StringBuilder (" go "), append (" od "). The toString ();

System. The out. Println (Java. Intern ()==Java);//false
System. The out. Println (good. Intern ()==good);//true

CodePudding user response:

Check the intern () method you will know that you use. Try equals

CodePudding user response:

See the JDK version

CodePudding user response:

Java already nearly built-in JVM

CodePudding user response:

refer to the original poster songleong response:
String Java=new StringBuilder (" ja "), append (" va "). The toString ();
String good=new StringBuilder (" go "), append (" od "). The toString ();

System. The out. Println (Java. Intern ()==Java);//false
System. The out. Println (good. Intern ()==good);//true


Java keywords already exist, this is no need to study more,

CodePudding user response:

https://blog.csdn.net/yanghan1222/article/details/80155711


CodePudding user response:

Piss me off, on talent!!!!!
 String Java=new StringBuilder (" ja "), append (" va "). The toString (); 
System. The out. Println (Java=="Java");

Results to false, although the content of the two String on the inside are the same, but they are not an object, this is the foundation, should be able to know why not equal! What, don't know? B: ok, I still say, understand the ability to solve problems,
Strings str1=new StringBuilder (" ja "), append (" va "). The toString ();//this is to create a new String object
String str2="Java";//using the literal constants, the system will look from the object pool first ever "Java objects, any direct return, no, you can create a, and on the object in the pool, and then return to this object
Java String str3=new String (" ");//here also use the "Java" literal constants, it just like str2, then new String (" Java ") using the "Java" object and create a new String object, so this one line of code that can produce two objects,
Str1 and str2 and str3 here are three different objects, if you use the==comparison is certainly false, OK, based on end, then it is easy to understand what you said the question why,
 String STR=new String (" Java "); 
System. The out. Println (STR==STR. Intern ());//false

Why is false, would then look at the intern () method of the document:

Key point: "when the intern method, if the pool already contains a String equal to this String Object (the Object by the equals (Object) method to determine), it returns the String" in the pool,
This suggests that the object in the pool, there was a "Java objects, Java intern () returns the object in the pool, and your Java object is an object of a new new, two objects certainly doesn't equal,
With good try:
 String STR=new String (" good "); 
System. The out. Println (STR==STR. Intern ());//false

Why is false is false, this need not say more, strangely, why are you the good output is true?
 String STR=new StringBuilder (" go "), append (" od "). The toString (); 
System. The out. Println (STR==STR. Intern ());//true

Here the results to true show?? Illustrates the object pool in the object returned and STR object is the same, but the STR object is just the new, explanation STR. Intern (), there is no equals the STR object pool of objects, so they put the STR object pool, and then return the STR object, so must be equal,
Look at the new String (" good ") are not equal:
String STR=new String (" good ");//using the "good" literal constants, there is no this object, object pooling is put the object in the pond, and then use the "good" and create a new object of STR, STR and the "good" is not the same object in the pool!
System. The out. Println (STR==STR. Intern ());//false, STR. Intern (), the pool has a "good" object, its and STR is not the same, so the result is false
  • Related