This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Confusion over intern of String class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confusion over intern of String class" Watch "Confusion over intern of String class" New topic
Author

Confusion over intern of String class

amit sharma
Ranch Hand

Joined: Jul 19, 2006
Posts: 129
I read that when we use intern funtion of String class then it not create any new object but instead give only referance.I write a simple program


What i expect that s1==s3 and s3==s4 both gives Equals but only s3==s4 gives equals .I didn't understand the reason.
Thanks
Keerthi Sagar
Greenhorn

Joined: Apr 21, 2007
Posts: 28
String.intern() returns a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.

String.intern() returns a canonical representation for the string object. A pool of strings, initially empty, is maintained privately by the class String.

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

All literal strings and string-valued constant expressions are interned. String literals are defined in �3.10.5 of the Java Language Specification.

In one way we can say that s3 and s4 will contains the same type of data i.e. the reference of the string s1. Where as s1 and s3 are not contains the same data.


Thanks and Regards,<br />Y. Keerthi Sagar.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by dmay chug:
I read that when we use intern funtion of String class then it not create any new object but instead give only referance.


Where did you read that? It's wrong.

Anyway, that's orthogonal to what your example code is actually testing: the first line already creates *two* String objects: one for the String literal (which is put into the constant pool), and one for the call to the constructor (which is *not* put into the pool) which is then referenced by s1. When you call intern() on it, the reference to the instance in the pool is returned, which is different from the instance created by the constructor.

Does that help?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[dmay]: I read that when we use intern funtion of String class then it not create any new object but instead give only referance.

[Ilja]: Where did you read that? It's wrong.


Unfortunately, that's pretty much what the API for String.intern() says:
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

The reality is a little different: if a String is not already in the intern pool, a new String is created is a special area of the heap (the permanent generation, for those who care) and a reference to the new String is returned.

So, in practice intern() may create a new String, or not. But it really doesn't matter for most of us. Programmers don't need to know whether it's a new String or not. It's irrelevant. Maybe it's a new String, maybe not, but either way, the String has been interned, and we should use the reference to the new String and forget about the old one (if there is any difference).


"I'm not back." - Bill Harding, Twister
amit sharma
Ranch Hand

Joined: Jul 19, 2006
Posts: 129
Originally posted by Ilja Preuss:


Where did you read that? It's wrong.

Anyway, that's orthogonal to what your example code is actually testing: the first line already creates *two* String objects: one for the String literal (which is put into the constant pool), and one for the call to the constructor (which is *not* put into the pool) which is then referenced by s1. When you call intern() on it, the reference to the instance in the pool is returned, which is different from the instance created by the constructor.

Does that help?

What i understand from your reply that when in line
String s1=new String("Hello");
it creates a string literal in constant pool and string object which point to that string literal then it return the referance of that string object .
When i write String s1="Hello"
String s2=s1.intern();
then s1==s2 gives true.
One more question let i already have Hello string literal in pool then when i call String s1=new String("Hello") then it create one more string literal
or not
thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Confusion over intern of String class