• Post Reply Bookmark Topic Watch Topic
  • New Topic

String's intern() method  RSS feed

 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I understand that the intern() method is to be used when you create Strings using the new operator and you want the 2 strings to be the same object. But in that case, why can't we just enclose the 2 strings in double quotes instead of using the new operator? I am trying to understand the usefulness of the intern method; I know I'm missing something here.
 
Paul Clapham
Sheriff
Posts: 22813
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Raman wrote:I understand that the intern() method is to be used when you create Strings using the new operator and you want the 2 strings to be the same object.


What you are missing is that your original assumption, which I quoted there, is incorrect. The intern() method can be applied to any Strings at all no matter how they were created.

But in that case, why can't we just enclose the 2 strings in double quotes instead of using the new operator?


I don't think you could always do that. For example how would you apply your double-quote idea to this code:



As for the usefulness of the method, I can't comment on that because I've never found it to be useful. No doubt there are use cases for it, or at least no doubt there were when Java was first designed.
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you,Paul. Kindly give me an example where it can be used meaningfully; I don't understand all of what you mean when you say that it can be applied to strings created in whatever way.
 
Paul Clapham
Sheriff
Posts: 22813
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


or...

 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the intern() statement do in your example? Does it make the string be available in the string pool?
 
Paul Clapham
Sheriff
Posts: 22813
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I suggest that you look in the API documentation, if you want to know what a method does? In this case the documentation answers your question very clearly.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:

or...


Doing either of these will not do anything useful.

Note that intern() returns a value, but you are not using that value. Just calling data.intern() will not automatically make data refer to the interned string. You would have to do data = data.intern();.

Note that in practice, you rarely (if ever) have to call intern() on strings. This is really only for very special cases; I can't remember ever having to use this method in 15 years of Java programming.

Don't call intern() just because you have some vague idea that it optimizes things. Only use it if you have some proof that it is actually beneficial for your program. Interning lots of strings can actually create a memory leak in your program.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Note that intern() returns a value, but you are not using that value. Just calling data.intern() will not automatically make data refer to the interned string. You would have to do data = data.intern();.

Or indeed, from the original example:
String data = getNameFromDatabase().intern();

Good point though.

Winston
 
Paul Clapham
Sheriff
Posts: 22813
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Just calling data.intern() will not automatically make data refer to the interned string. You would have to do data = data.intern();.


On the other hand just calling data.intern() does have the (possible) side effect of putting the string into the string pool. That might be useful in some cases, I suppose.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!