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 ?

tbodt
  • 16,609
  • 6
  • 58
  • 83
Ninad Pingale
  • 6,801
  • 5
  • 32
  • 55
  • 1
    For what purpose? How is it going to affect the running of your program? – user207421 Jun 05 '14 at 12:06
  • 1
    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. – Ninad Pingale Jun 05 '14 at 12:12
  • Possible duplicate: http://stackoverflow.com/questions/19049812/can-we-access-or-query-the-java-string-intern-constant-pool – Vince Jun 19 '14 at 13:33
  • 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(). – NomadMaker Aug 26 '20 at 21:04

1 Answers1

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.

izstas
  • 5,004
  • 3
  • 42
  • 56
  • 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().` – user207421 Jun 05 '14 at 12:08
  • I understand that, but as far as I know the actual string pool is still the same. – izstas Jun 05 '14 at 12:46
  • 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. – user207421 Jun 05 '14 at 13:05
  • Well, I guess I was meant to say "from my understanding of the JLS". – izstas Jun 05 '14 at 13:39
  • 3
    @EJP: that must be a *very early* JDK. [I found a JDK1.2 documentation](http://www.cs.mun.ca/~michael/java/jdk1.2-docs/api/java/lang/String.html#intern()) 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?](http://stackoverflow.com/questions/2431540/garbage-collection-behaviour-for-string-intern#comment2416536_2431597) – Holger Jun 20 '14 at 07:36