• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion over intern of String class

 
amit sharma
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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).
 
amit sharma
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic