• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

intern() method

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
can anyone please explain intern() method by giving examples here?
I am getting confused in it also.

Thanks
 
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Bert moved it to proper forum...
 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

extra string will still be created but yes s will point to string in pool.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic