• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on values in String Pool  RSS feed

 
Abhishek Chhawcharia
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Will the below line, when executed will put the value of string "Java" in the string constant pool?

String str = new String ("Java");

Appreciate your answer.

Abhishek

 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You tell us what you think and we shall tell you whether we agree.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is something you don't actually need to know
 
Abhishek Chhawcharia
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read about this in Mala Gupta's books and also read in Kathy Sierra and got confused. I think as String objects are immutable so this should also go in constant pool and later could be reused if a new object is being created as below.

String = "Java";

 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That doesn't answer your question: does that String go into the String pool when that line is executed?
 
Abhishek Chhawcharia
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ritchie,

What I meant was that Java as string even when executed with new willl go in string constant pool. is this correct?
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't understand, but that looks to me like a different question. The original question was whether the String "Java" goes into the String pool when that code is executed.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tell us whether you think that String will go into the String pool and when.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Tell us whether you think that String will go into the String pool and when.

and anything else that might happen...
 
Abhishek Chhawcharia
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes String Java will go in string pool once the below line is executed.

String str = new String ("Java");

 
Abhishek Chhawcharia
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String Java will go in string pool when the below line is executed.

String str = new String ("Java"); // line 1

And after line 1, if there is line 2 as below

String str1 = "Java"; // line 2

then when line 2 executes, no new string will be created in string pool as Java from line is already existing in pool.
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhishek, as I see you're a bit confused.
Some visual information should help you understand.

Look what I found for you on www.: http://examples.javacodegeeks.com/core-java/lang/string/java-string-pool-example/ and http://www.journaldev.com/797/what-is-java-string-pool
 
Abhishek Chhawcharia
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

I guess, yes I was confused. I am still learning basics of java.

Thanks for the links, so the objects are not created in the pool when created with new operator. But the literal value of the string will be kept in the string pool. This helps.


 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that String is already in the String pool before that line runs; it is put into the String pool the first time a class containing that particular String literal (or an equal constant expression) is loaded.
Line 2 is a simple assignment; as you know the String already exists in the String pool.
 
Abhishek Chhawcharia
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Ritchie,

links that you gave just made the concept clear to me. Appreciate your help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!