aspose file tools
The moose likes Beginning Java and the fly likes Why there are different references of the actual String Object and its interned Object Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Why there are different references of the actual String Object and its interned Object" Watch "Why there are different references of the actual String Object and its interned Object" New topic
Author

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

Amit Kehri
Greenhorn

Joined: Aug 01, 2012
Posts: 2
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" ?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 3793
    
    1

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).
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 4753
    
    7

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


Isn't it funny how there's always time and money enough to do it WRONG?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32689
    
    4
…and welcome to the Ranch
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Why there are different references of the actual String Object and its interned Object
 
Similar Threads
String intern()
Strings in java.
string questions?
intern() ?
doubt on garbage collection