• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why there are different references of the actual String Object and its interned Object

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is said that intern() of String called on a String Object, first searches the same Object through its equals() in all available references in literal pool, and if not found then puts a reference of that String Object in literal pool , and returns the same reference to calling application.


String heapString = new String("Hello");
String internedString = heapString.intern();
System.out.println(heapString == heapInternedString ? "Same Reference":"Different Reference");

Output comes "Different Reference".

As we know that String constant pool contains references of the Strings Objects allocated in heap not actual String Objects.
Here 'heapString' is on heap , and its reference is put in String literal pool when intern() is called on this. Same reference is returned to internedString. Then why I am getting result as "Different References" ?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Amit. Welcome to the Ranch!

Amit Kehri wrote: Here 'heapString' is on heap , and its reference is put in String literal pool when intern() is called on this.


A String with that value is put in the pool, but that doesn't change the heapString reference (since you never assign anything new to that). So your comparing a reference in the pool with one that's not in the pool - they're bound to be different.

(I assume the fact that one line refers to internedString and one refers to heapInternedString is a typo).
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Amit Kehri wrote:Then why I am getting result as "Different References" ?


Because new String("Hello") (indeed, new String(anything)) will never return you a String from the pool.

If you'd written
String heapString = "Hello";
your result would have been different.

Winston
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
…and welcome to the Ranch
 
reply
    Bookmark Topic Watch Topic
  • New Topic