aspose file tools*
The moose likes Beginning Java and the fly likes What is String.intern ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is String.intern ?" Watch "What is String.intern ?" New topic
Author

What is String.intern ?

Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
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?


comp.lang.javascript FAQ: http://jibbering.com/faq/
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
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 ]

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
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

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

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?


Still a beginner at crafting quality coding but have the zeal to learn more. Your help would be appreciated.
My Blog on Learning Analytics
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8207
    
  23

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1823
    
    7

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.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8207
    
  23

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
 
 
subject: What is String.intern ?