What makes reference comparison (==) work for some strings in Java?

不羁的心 提交于 2019-11-26 02:54:54

问题


I have following lines of codes to compare String. str1 not equal to str2, which is understandable since it compares object reference. But then why s1 is equal to s2?

String s1 = \"abc\";
String s2 = \"abc\";

String str1 = new String(\"abc\");
String str2 = new String(\"abc\");

if (s1==s2)
    System.out.println(\"s1==s2\");           
else
    System.out.println(\"s1!=s2\");

if (str1==str2)
    System.out.println(\"str1==str2\");           
else
    System.out.println(\"str1!=str2\");

if (s1==str1)
    System.out.println(\"str1==s1\");         
else
    System.out.println(\"str1!=s1\");

Output:

  s1==s2
  str1!=str2
  str1!=s1 

回答1:


The string constant pool will essentially cache all string literals so they're the same object underneath, which is why you see the output you do for s1==s2. It's essentially an optimisation in the VM to avoid creating a new string object each time a literal is declared, which could get very expensive very quickly! With your str1==str2 example, you're explicitly telling the VM to create new string objects, hence why it's false.

As an aside, calling the intern() method on any string will add it to the constant pool (and return the String that it's added to the pool.) It's not necessarily a good idea to do this however unless you're sure you're dealing with strings that will definitely be used as constants, otherwise you may end up creating hard to track down memory leaks.




回答2:


s1 and s2 are String literals. When you create a new String literal the compiler first checks whether any literal representing the same is present in the String pool or not. If there is one present, the compiler returns that literal otherwise the compiler creates a new one.

When you created String s2 the compiler returns the String s1 from the pool as it was already created before. That is the reason why s1 and s2 are same. This behaviour is called interning.




回答3:


This phenomenon is due to String interning.

Basically, all string literals are "cached" and reused.




回答4:


This is due to String literals being interned. On this matter, Java documentations says:

All literal strings and string-valued constant expressions are interned

And this explains why s1 and s2 are the same (these two variables point to the same interned string)




回答5:


In Java, the same constant strings will be reused. So that s1 and s2 point to the same "abc" object and s1==s2. But when you use new String("abc"), another object will be created. So that s1 != str1.




回答6:


As string is immutable in java, all the string literals cached for the reusability.

When you create String object using new() operator, it always create a new object in heap memory. On the other hand, if you create object using String literal syntax e.g. "Java", it may return an existing object from String pool (a cache of String object in Perm gen space, which is now moved to heap space in recent Java release), if it's already exists. Otherwise it will create a new string object and put in string pool for future re-use.

String s1 = new String("java");
String s2 = new String("java");
String s3 = "java";
String s4 = "java";

Please refer this link



来源:https://stackoverflow.com/questions/9698260/what-makes-reference-comparison-work-for-some-strings-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!