Reputation: 5042
java docs says:
A pool of strings, initially empty, is maintained privately by the class String.
1) Is it a pool of string literals
or references
to these string literals? on net some articles refer it as pool of strings literals while other refer it as pool of references so i got confused.
2) Is string pool created per class basis or per JVM basis?
3)Is there any reference where i can find details of string pool, its implementation etc.?
Upvotes: 1
Views: 319
Reputation: 719596
1) Is it a pool of string literals or references to these string literals? on net some articles refer it as pool of strings literals while other refer it as pool of references so i got confused.
It is the same thing. You can't have a String object without a reference, or vice-versa.
And, as Peter Lawrey puts it: "In Java, an Object is inside the heap. Nowhere else. The only thing you can have inside something else, an object, an array, a collection or the stack, is a reference to that object."
2) Is string pool created per class basis or per JVM basis?
There is one String pool per JVM ... unless you are using some exotic JVM where they've decided to implement it differently. (The spec doesn't say that there has to be one string pool for the JVM, but that's generally the most effective way to do it.)
3)Is there any reference where i can find details of string pool, its implementation etc.?
You can download the complete source code of OpenJDK 6 or 7. The spring pool is implemented in native code ... so you'll be reading C++.
Upvotes: 4
Reputation: 2190
Is it a pool of string literals or references to these string literals?.
well, obviously it is pool of string literals. suppose you write,
String str= "a learner";
It will search in String pool by equals() method whether the same string is there in string pool or not.If it is there in Pool, that String object is returned, otherwise it is stored in String Pool and a reference to newly added string is returned.
So , it is pool of String objects, on which equals() method is called whenever you type a new string literal.
Is string pool created per class basis or per JVM basis?
There can be only one class of String in JVM because String class is final. So there is no question of more than one String class per JVM. Ultimately it comes out to be only one String Pool per JVM.
Upvotes: 1
Reputation: 425368
It's called String interning.
Upvotes: 0