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 what is difference between using String intern method and this way of creating string.? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "what is difference between using String intern method and this way of creating string.?" Watch "what is difference between using String intern method and this way of creating string.?" New topic
Author

what is difference between using String intern method and this way of creating string.?

Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
I understand that there is difference between using string Intern method and creating string as String temp=new String("hello");

But I am not able to understand difference between using string intern and creating string as Sring temp ="hello".
thanks.
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

I do not think that there is any difference. I believe that
Sring temp ="hello"; is also interned by the JVM by calling the intern() method to send it to constant pool
OR probably, the argument from String temp = new String("hello") is used to call intern() method for interning the constant to string literal pool.
The change is not made in bytecode as javap did not show this when I looked. Actually, I am not very confident on this, Java Language Specification - String Literals might be of help.

The best question should have been, though: How and when should I use intern() method?


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
I do not think that there is any difference


That is the reason this doubt arised.If there is no difference then why is intern used?

thanks
Kumara Undru
Greenhorn

Joined: Jul 22, 2013
Posts: 2
I believe, string intern is - to move "new String()" to string pool. so that, calling intern method on string literals doesn't make any change.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

There is no difference between calling intern() on a string literal and using the string literal itself, that's true. But consider what happens when you call intern() on a value which isn't a string literal.
Kumara Undru
Greenhorn

Joined: Jul 22, 2013
Posts: 2
calling intern method on string literals doesn't make any change.


Non-literals will become literals when intern is invoked on non-literals.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Kumara Undru wrote:Non-literals will become literals when intern is invoked on non-literals.


That's a strange way of explaining what actually happens. Which is that the value of the string passed to the intern() method is added to the string pool.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Monica. Shiralkar wrote:I understand that there is difference between using string Intern method and creating string as String temp=new String("hello");

But I am not able to understand difference between using string intern and creating string as Sring temp ="hello".

The fact is that you rarely need to use intern(), and almost never with straight literals. If you're combining strings, however, you can use it to see if the combination has already been created; although in many cases, that's so unlikely that you're just wasting CPU cycles.

The CachedObjects page contains some examples that might be helpful.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
thanks all
 
GeeCON Prague 2014
 
subject: what is difference between using String intern method and this way of creating string.?