Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q for regardinging intern() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Q for regardinging intern()" Watch "Q for regardinging intern()" New topic
Author

Q for regardinging intern()

Jennifer Warren
Ranch Hand

Joined: Aug 24, 2001
Posts: 53
Well this is in additon to one of the question asked previously. I wanted to know if u do as following.
String s = new String("Java");
s.intern();
String s = "Java";
s==s1 still returns false.
why!.
cause if you have two strings like,
String s1="java";
String s2="java";
s1==s2 will always return true due to this string being in the pool not in the heap.
so what is the significance of intern() method.
Jennifer.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
because invoking intern on s does not reassign the reference hold by s to the reference now contained in the pool.
try doing this and it works
s = s.intern();
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Q for regardinging intern()
 
Similar Threads
Comparing Strings
Doubt in Strings
Quick == question
String intern (); method
String Immutable!