File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Java Interview Guide this week in the Jobs Discussion 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: 9059
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
jQuery in Action, 3rd edition