How to check String Pool Contents?
Asked Answered
W

1

29

Is there any way to check, currently which Strings are there in the String pool.

Can I programmatically list all Strings exist in pool?

or

Any IDE already have this kind of plugins ?

Wayless answered 5/6, 2014 at 6:57 Comment(4)
For what purpose? How is it going to affect the running of your program?Babarababassu
While learning String Pooling concept,I found examples in tutorials saying this string will go in string pool and this one not. So I thought if this concept can be made more clear by printing existing strings in the pool. I don't require it in any assignment.Wayless
Possible duplicate: #19050312Klug
The string constant pool is there is create a bit of efficiency for constant strings that can be known at compile-time. Other than that it doesn't matter. You should always compare strings with equals().Catboat
A
19

You are not able to access the string pool from Java code, at least not in the HotSpot implementation of Java VM.

String pool in Java is implemented using string interning. According to JLS §3.10.5:

a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern.

And according to JLS §15.28:

Compile-time constant expressions of type String are always "interned" so as to share unique instances, using the method String.intern.

String.intern is a native method, as we can see in its declaration in OpenJDK:

public native String intern();

The native code for this method calls JVM_InternString function.

JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
    JVMWrapper("JVM_InternString");
    JvmtiVMObjectAllocEventCollector oam;
    if (str == NULL) return NULL;
    oop string = JNIHandles::resolve_non_null(str);
    oop result = StringTable::intern(string, CHECK_NULL);
    return (jstring) JNIHandles::make_local(env, result);
JVM_END

That is, string interning is implemented using native code, and there's no Java API to access the string pool directly. You may, however, be able to write a native method yourself for this purpose.

Azole answered 5/6, 2014 at 10:35 Comment(5)
The use of the word 'intern' in the JLS is extremely confusing, as what is being described there is really compile-time string pooling. It has precisely nothing to do with String.intern().Babarababassu
I understand that, but as far as I know the actual string pool is still the same.Azole
There being no way of telling, as per your answer, short of reading the HotSpot source code, and taking into account what happens in other vendors' JVMs, there is no way you can know that. In earlier JDKs the string pool was a Map in java.lang.String, and therefore completely separate from the constant pool. So at one time at least your 'knowledge' was false. Now it's something opaque. That's all you can actually know.Babarababassu
Well, I guess I was meant to say "from my understanding of the JLS".Azole
@EJP: that must be a very early JDK. I found a JDK1.2 documentation and it clearly says: “All literal strings and string-valued constant expressions are interned.” I don’t remember a Java Language Specification ever saying something different. I can’t imagine the purpose of another distinct String pool. And, by the way did you say that?Rainbow

© 2022 - 2024 — McMap. All rights reserved.