• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on values in String Pool

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You tell us what you think and we shall tell you whether we agree.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tell us whether you think that String will go into the String pool and when.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Ritchie,

links that you gave just made the concept clear to me. Appreciate your help.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic