The return of String.intern() explained

邮差的信 提交于 2020-01-30 04:07:11

问题


Consider:

String s1 = new StringBuilder("Cattie").append(" & Doggie").toString();
System.out.println(s1.intern() == s1); // true why?
System.out.println(s1 == "Cattie & Doggie"); // true another why?

String s2 = new StringBuilder("ja").append("va").toString();
System.out.println(s2.intern() == s2); // false

String s3 = new String("Cattie & Doggie");
System.out.println(s3.intern() == s3); // false
System.out.println(s3 == "Cattie & Doggie"); // false

I got confused why they are resulting differently by the returned value of String.intern() which says:

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

Especially after these two tests:

assertFalse("new String() should create a new instance", new String("jav") == "jav");
assertFalse("new StringBuilder() should create a new instance",
    new StringBuilder("jav").toString() == "jav");

I once read a post talking about some special strings interned before everything else, but it's a real blur now.

If there are some strings pre-interned, is there a way to get kind of a list of them? I am just curious about what they can be.


Updated

Thanks to the help of @Eran and @Slaw, I finally can explain what just happened there for the output

true
true
false
false
false
  1. Since "Cattie & Doggie" doesn't exist in the pool, s1.intern() will put the current object reference to the pool and return itself, so s1.intern() == s1;
  2. "Cattie & Doggie" already in the pool now, so string literal "Cattie & Doggie" will just use the reference in pool which is actually s1, so again we have true;
  3. new StringBuilder().toString() will create a new instance while "java" is already in the pool and then the reference in pool will be returned when calling s2.intern(), so s2.intern() != s2 and we have false;
  4. new String() will also return a new instance, but when we try to s3.intern(), it will return the previously stored reference in the pool which is actualy s1 so s3.intern() != s3 and we have false;
  5. As #2 already discussed, String literal "Cattie & Doggie" will return the reference already stored in the pool (which is actually s1), so s3 != "Cattie & Doggie" and we have false again.

Thanks for @Sunny to provide a trick to get all the interned strings.


回答1:


s2.intern() would return the instance referenced by s2 only if the String pool didn't contain a String whose value is "java" prior to that call. The JDK classes intern some Strings before your code is executed. "java" must be one of them. Therefore, s2.intern() returns the previously interned instance instead of s2.

On the other hand, the JDK classes did not intern any String whose value is equal to "Cattie & Doggie", so s1.intern() returns s1.

I am not aware of any list of pre-interned Strings. Such a list will most likely be considered an implementation detail, which may vary on different JDK implementations and JDK versions, and should not be relied on.




回答2:


When the intern() method is invoked on a String object it looks the string contained by this String object in the pool, if the string is found there then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

So java string must already be in the pool. hence it is giving false.

You can print all strings in pool

How to print the whole String pool?

Here is an example to get all string if you are using openjdk.




回答3:


String literals (those that are hardcoded like "a string") are already interned for you by the compiler. But those strings that are acquired programmatically are not, and will be interned only if you use .intern() method.

Usually you don't intern strings manually, unless you know you will store in memory a large number of repeating strings, so you can save a lot of memory that way.

That is explained here: What is Java String interning?



来源:https://stackoverflow.com/questions/55135968/the-return-of-string-intern-explained

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