aspose file tools*
The moose likes Beginning Java and the fly likes Regarding intern() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regarding intern() method " Watch "Regarding intern() method " New topic
Author

Regarding intern() method

surya.raaj prakash
Ranch Hand

Joined: Oct 30, 2009
Posts: 76
Hi Friends,

What is use of intern() method in String class ?I'm confusing with this method,please explain to me with an example.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3102
    
  40

From the documentation...
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.


What particular thing of this is not so clear for you?


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
surya.raaj prakash
Ranch Hand

Joined: Oct 30, 2009
Posts: 76
Hi Devaka,
Thanks For your Replay.

intern() Returns a canonical representation for the string object.


What is mean of canonical representation of string object?please explain it.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3102
    
  40

It means the standard String representation. It is well explained in the second paragraph

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.
surya.raaj prakash
Ranch Hand

Joined: Oct 30, 2009
Posts: 76
Thanks For Your Reply,

I have one more doubt.



From The preceding code the string s1 will be created in string pool but String object created with new operator,the JVM always creates new object in heap without looking at string pool and creates a new String instance each time it is executed.
My Question is when you compare s1 and s2 (s1 == s2) you will get false but when you call intern() method on s2 (s1 == s2.intern()) you will get true.
How intern() method works with String Literals and new operator?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38896
    
  23
Try itTll us what happens. What happens to the String originally associated with cr?
surya.raaj prakash
Ranch Hand

Joined: Oct 30, 2009
Posts: 76

Hi Cambell,
Thanks For Your Reply.

I have got the output message is:
The two Strings are not the same object.
The two Strings are the same object after using the intern method.


Campbell Ritchie wrote: What happens to the String originally associated with cr?

After intern method cr is referce to String pool object.
surya.raaj prakash
Ranch Hand

Joined: Oct 30, 2009
Posts: 76
surya.raaj prakash wrote:
Hi Cambell,
Thanks For Your Reply.

I have got the output message is:
The two Strings are not the same object.
The two Strings are the same object after using the intern method.


Campbell Ritchie wrote: What happens to the String originally associated with cr?

After intern method cr is referce to String pool object.


please correct me if I'm wrong
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38896
    
  23
The bit about cr means the String which was first associated with cr, ie that created with new. You should be able to work out what intern() does from your outputs.
surya.raaj prakash
Ranch Hand

Joined: Oct 30, 2009
Posts: 76
Hi Cambell,
Will it save the memory used by the objects?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38896
    
  23
What does "save the memory" mean?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

I doubt it will "save memory". In fact I believe it will even increase memory usage. From the Javadocs (bold added by me):
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.

So if the String was not in the String pool yet it will be added. Even if this is just one single reference it is still a little bit of extra memory required.

To be honest, I believe you can go your entire programming career without ever calling intern(). The only reason I see for using it is being able to use ==. I never do that anyway, I always use the equals method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
wood burning stoves
 
subject: Regarding intern() method