This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes use of Intern() method of String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "use of Intern() method of String" Watch "use of Intern() method of String" New topic

use of Intern() method of String

Noorulain Khan

Joined: Jul 31, 2001
Posts: 28
What is the use of intern method with string while considering on heap and pool
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9056
intern() causes the String to go into the String pool.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
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.

"JavaRanch, where the deer and the Certified play" - David O'Meara
I agree. Here's the link:
subject: use of Intern() method of String
It's not a secret anymore!