File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes intern() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "intern() method" Watch "intern() method" New topic
Author

intern() method

Prakash Mahto
Ranch Hand

Joined: Nov 07, 2010
Posts: 37
Hi
can anyone please explain intern() method by giving examples here?
I am getting confused in it also.

Thanks
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

intern() method is a part of String class so you can call intern on String objects

The result of calling this method is that the strings are moved to the literal pool.

So next time you create a string literal object, JVM will find it in the heap.



Javadoc for intern() method will make it more clear to you.


String java.lang.String.intern()

intern
public 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


Returns:
a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.


Java Experience
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8813
    
    5
It's an interesting topic, but it's not on the exam.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

Yes, Bert moved it to proper forum...
Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
Err, just out of curiousity, how can you be sure this isn't on the exam? The method was atleast mentioned in my course-book whichs purpose is solely to prepare for the SCJP and SCJD.

I'm also curious about the following...

If you declare a String like this:

String s = new String("hi");

This will create a new String object in the program space (I believe). Now, if we assume the string "hi" is already in the string pool, and you issue the following:

String s = new String("hi).intern()

Will this in fact make 's' point to the string in the literal pool, and also avoid the creation of the extra String object in the program space?

// Andreas
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

hi,

extra string will still be created but yes s will point to string in pool.
Rafal Sojka
Greenhorn

Joined: Mar 31, 2009
Posts: 4
Hi,
I found really interesting webpages that explain many nuances of the String literal pools and the intern() method:
- What is String literal pool?
- String is special (Look at the "String Literal vs. String Object" subsection.)

I hope it helps you a little.
Best regards,
foxrafi
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14103
    
  16

Andreas Svenkson wrote:Err, just out of curiousity, how can you be sure this isn't on the exam? The method was atleast mentioned in my course-book whichs purpose is solely to prepare for the SCJP and SCJD.

Bert is one of the authors of the SCJP Study Guide and also worked with Sun on the actual exam itself. You can trust him to know exactly what is and is not on the SCJP exam.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Prakash Mahto
Ranch Hand

Joined: Nov 07, 2010
Posts: 37
Thanks everyone for explaining intern() and making me clear that it is not on the exam
Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
Jesper de Jong wrote:
Andreas Svenkson wrote:Err, just out of curiousity, how can you be sure this isn't on the exam? The method was atleast mentioned in my course-book whichs purpose is solely to prepare for the SCJP and SCJD.

Bert is one of the authors of the SCJP Study Guide and also worked with Sun on the actual exam itself. You can trust him to know exactly what is and is not on the SCJP exam.


Good to know ^^

// Andreas
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: intern() method