Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

intern() method

 
Prakash Mahto
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
can anyone please explain intern() method by giving examples here?
I am getting confused in it also.

Thanks
 
Sunny Bhandari
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's an interesting topic, but it's not on the exam.
 
Sunny Bhandari
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Bert moved it to proper forum...
 
Andreas Svenkson
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

extra string will still be created but yes s will point to string in pool.
 
Rafal Sojka
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15438
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Prakash Mahto
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for explaining intern() and making me clear that it is not on the exam
 
Andreas Svenkson
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic