1

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.?

a Learner
  • 4,944
  • 10
  • 53
  • 89
  • 2
    1) Pool of references to String literals. 2) The pool is created per JVM basis. 3) You can download the JDK source to check how the pool is implemented. – Luiggi Mendoza Jun 24 '12 at 06:26
  • Check this. may be useful http://stackoverflow.com/questions/8046045/java-string-literal-pool-and-string-object – chaosguru Jun 24 '12 at 06:51
  • @ Luiggi Mendoza:`Pool of references to String literals.` -So u mean pool contain only references and those references are pointing to string literals which are somewhere else in memory/heap.String literals/objects themselves are not in pool? – a Learner Jun 24 '12 at 07:17

3 Answers3

4

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++.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Though we can't have an object without reference but I think reference and object itself are two separate entities.Containing references only and containing whole object itself is not the same thing. – a Learner Jun 24 '12 at 07:24
  • 2
    @aLearner 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. – Peter Lawrey Jun 24 '12 at 10:32
  • What is "something else" except stack? – Gennady Vanin Геннадий Ванин Apr 05 '13 at 09:52
  • @GennadyVanin--Novosibirsk - the *"something else"* Peter is referring to is *"an object, an array, a collection or the stack"*. – Stephen C Apr 05 '13 at 12:15
1
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.

Ahmad
  • 2,110
  • 5
  • 26
  • 36
  • +1 for last two statements. But answer of first point is still unclear and your answer is conflicting with comment by Luiggi Mendoza above. – a Learner Jun 24 '12 at 07:27
  • @a Learner...well do you need a reference even if you want to traverse through each and every string objects in the Pool..?.reference will be needed only if you need to access a particular object. I searched many articles on the net..none talks about reference pool.. a good analogy is Sets in Collection framework. They really don't need to maintain reference to every object they contain. – Ahmad Jun 24 '12 at 08:02
  • There are articles like `http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html` ,`http://blog.javainterview.co.in/` which talks about references and many other talk about pool of `string literals` only. Sets in Collection is good example. Though we are more concerned with String objects themselves I just wanted to clear this what actually pool contains. – a Learner Jun 24 '12 at 08:10
0

It's called String interning.

  1. It is a pool of String literals
  2. Interning is done on a JVM basis
  3. The JDK source for String has all the code in there
Bohemian
  • 412,405
  • 93
  • 575
  • 722