• 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

string created in heap

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

How to store string created in heap into string pool?


Thanks in advance
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String.intern()
 
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings priyanaka,
If you are using the new keyword to create a string , then the JVM creates a new object in the pool , the one that represents the literal of the string , and there will be a separate String object, not in the pool, that contains a copy of the content of the pooled object. However if you avoid using the new keyword , then JVM will be smart enough to re-use the same string object.

 
Saif Asif
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stuart A. Burkett wrote:String.intern()



Oh yeah . Much simpler and easy . It will push it in the pool .
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The intern method has the advantage that it works on Strings which are not literals or compile‑time constants. You can enter a String like "Saif" at the command line or wherever and it will still be interned correctly.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saif Asif wrote:Greetings priyanaka,
If you are using the new keyword to create a string , then the JVM creates a new object in the pool



No. The new keyword never causes an entry to be added to the pool.

the one that represents the literal of the string , and there will be a separate String object, not in the pool, that contains a copy of the content of the pooled object.



So what really happens is:

1. If there is a String literal, it goes into the pool, regardless of whether it's every used as an arg to a String constructor.

2. If you use the new keyword to create a String, that never causes an entry to be placed into the pool, regardless of whether the parameter to the constructor is a literal.





Note that executing that line of code does not create an entry in the pool. The "123" entry will be placed in the pool when the class is initialized, if it wasn't already there. By the time we get to execute this line, the literal will already be in the pool.



Same here. Those comments are incorrect. It will be placed in the pool before we ever get here.
 
Saif Asif
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cambell wrote: The intern method has the advantage that it works on Strings which are not literals or compile‑time constants. You can enter a String like "Saif" at the command line or wherever and it will still be interned correctly.

So the intern() method is to be used whenever we want any String to be inserted into the pool ?

Jeff , thank you for the detailed explanation , cleared a bit of my misunderstanding too . Another thing , these 2 lines of code

String a = "123" //Is it in pool? If yes then get that , else create new object
String b = "123" //Is it in pool ? yes ? get it and DON'T create new object


are separate from the first 2 lines. I had typed them in the same code tags and that made them look as they were part of the same code snippet. Being seperate from the other lines , I think the comments are correct.

First line will check to see if the String literal is in the pool or not . Since its not in the pool , it will create a new object , copy one in the pool and send the reference back. When the next line is executed , the same String literal is being referenced and it will be searched for in the pool as before only now that the JVM will find it in the pool and return the reference directly ( without creating a new object on the heap ). ( Do correct me if I am wrong here )
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saif Asif wrote:

First line will check to see if the String literal is in the pool or not .



No.

As I stated in my previous post (although perhaps not clearly or explicitly enough--apologies for that), any String literal in the code is put into the pool when the class is initialized (if it's not already there). Executing lines like the above never checks the pool for existence or puts anything into the pool.


 
Saif Asif
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it . Thank you Jeff for the explanation , Its clear now :-)
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that in a real Java program you should never use the new String(...) constructor with a string literal (as in: new String("hello")). If you do that, you are unnecessarily creating a new String object explicitly, which voids the entire purpose of the string pool.

Also, you practically never call the intern() method on a string. I can't remember ever using it for any serious program I've written in the past 15 years.

So, while it's good to know how it works, you'll rarely ever need to bother with this in real-world programs.
 
priyanaka jaiswal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you All for great explanation and clearing my doubt.
 
reply
    Bookmark Topic Watch Topic
  • New Topic