• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

use of Intern() method of String

 
Noorulain Khan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the use of intern method with string while considering on heap and pool
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
intern() causes the String to go into the String pool.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This would be important if the String had been created using the new operator and therefore was out on the heap - not a good place to be if you want to optimize by re-using the same object in multiple places.
For efficiency you might force that object into the pool using intern(). Now it is available to be referenced for any other STrings that have the same value.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic