0

All we know that JVM stores String variables in a separate StringPool. Every time when we create a String (eg. String s1 = "anystring") it stores as a constant & when we create new String variable as same value it uses same reference except creating new String Constant.

I just want to know that what is the life of that variable in StringPool. is it application level or JAVA environment level.

Question: If JVM level, then does different application uses same references?

I am not sure where I am up to in String theory. But I'm looking for answer.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Suresh Sharma
  • 1,826
  • 22
  • 41
  • `String` objects are also referenced/used somewhere in the code. It is the job of the Java Garbage Collector to collect the object references based on its convention. – Madhusudan Joshi May 08 '13 at 11:32
  • Is there a deeper reason you want to know this? I mean, you might be interested purely in the theory behind it all, but are you having some real-life problem for which you want find an answer? Normally you shouldn't worry about the StringPool, as long as all your code uses `"string".equals(otherString)` - in that case it's just there for efficiency. – JBert May 08 '13 at 11:42
  • JBert, i just want to know the difference between String & other data types & How JVM manages them differently. – Suresh Sharma May 08 '13 at 11:56

2 Answers2

2

The lifetime of an object in the string pool is governed by the same rules as any other object. An object will continue to exist as long as it is reachable. Some time after it becomes unreachable it will be reclaimed.

The only thing that is "different" about a String object that corresponds to literal in a class is that the object is reachable via the code for the class. Normally, the lifetime of the code of a class is the lifetime of the JVM. However, if the class was dynamically loaded, and you then proceed to make the classloader that loaded it unreachable, etcetera, then the class may be come unreachable, and hence the literal objects may become unreachable.

I just want to know that what is the life of that variable in StringPool. is it application level or JAVA environment level.

It is not clear what you mean by "application level" and "Java environment level", but under normal circumstances the lifetime of the running application is the same as the lifetime of the JVM. But either way, it is all determined by reachability analysis; i.e the "tracing" process that the GC uses to determine if an object can still be used in the computation.


Actually, this statement in your Question is arguably incorrect.

All we know that JVM stores String variables in a separate StringPool.

In fact, in Java 7 the string pool is actually stored in the regular heap, not the permgen heap. (And besides, you really means String objects not String variables. Objects and variables are not the same thing at all ...)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • This is really helpful, can you please describe a bit more about your comment in reference of "String Object is reachable via the code of the class unlike others". – Suresh Sharma May 08 '13 at 11:53
  • Yes, I realize that there is little mistake i made in questioning but basically i want to know that what's the difference the way of managing String Objects & others by JVM/GC. – Suresh Sharma May 08 '13 at 12:03
  • Simply, there is a reference in the code that points to the String literal object. There are not references in the code to (for example) objects that the code causes to be created. – Stephen C May 08 '13 at 12:04
  • The answer is that there is no difference. – Stephen C May 08 '13 at 12:04
0

String object are stored at application level in java, every string object is limited to its class scope.

Jignesh Parmar
  • 68
  • 1
  • 1
  • 9