Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is String.intern ?

 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
intern()
Returns a canonical representation for the string object.
I've never used it and I don't know what a canonical representation is. What is it what's it used for?
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The idea behind intern() is that there is one and only one interned string with a particular value. So if you intern a bunch of strings, you can later compare them using
== rather than equals().
The API documentation explains this more clearly.
[ October 13, 2002: Message edited by: Ron Newman ]
 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I see now. All String literals are interned. That's why String literals are == but Strings constructed w/new String are !=
Better explained in code:

Running:
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like you're understanding the concept well.
You may want to take a look at Roedy Green's Java Glossary on interned Strings for a detailed explanation as well as a gotcha to be aware of.
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dirk Schreckmann wrote:Looks like you're understanding the concept well.
You may want to take a look at Roedy Green's Java Glossary on interned Strings for a detailed explanation as well as a gotcha to be aware of.

Just an update, the url you provided does not work. Gives a 404 Not Foud error.
just googled it and found http://mindprod.com/jgloss/interned.html ...perhaps its the one that you are referring too?
Also, can you tell me Dirk, how to incorporate these smileys that fellow ranchers use in their post?
 
Winston Gutkowski
Bartender
Pie
Posts: 10254
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron Newman wrote:So if you intern a bunch of strings, you can later compare them using == rather than equals().

You can; but it's a really bad idea.

And for more info on that subject, read the AvoidTheEqualityOperator (←click) page.

Winston
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish Dutt wrote:
Dirk Schreckmann wrote:Looks like you're understanding the concept well.
You may want to take a look at Roedy Green's Java Glossary on interned Strings for a detailed explanation as well as a gotcha to be aware of.

Just an update, the url you provided does not work. Gives a 404 Not Foud error.
just googled it and found http://mindprod.com/jgloss/interned.html ...perhaps its the one that you are referring too?
Also, can you tell me Dirk, how to incorporate these smileys that fellow ranchers use in their post?


This topic and Dirk's reply were posted back in 2002, so don't hold you breath waiting for a response
The smilies you can add by clicking on the + symbol next to Emoticons just above the left upper corner of the input textfield.
 
Winston Gutkowski
Bartender
Pie
Posts: 10254
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:This topic and Dirk's reply were posted back in 2002, so don't hold you breath waiting for a response

Whoops. Don't I feel the idiot.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic