Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Monica Shiralkar
Ranch Hand
Posts: 826
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 207
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Monica Shiralkar
Ranch Hand
Posts: 826
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10109
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Monica Shiralkar
Ranch Hand
Posts: 826
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic