The return of String.intern() explained
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.
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 String
s 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.