aspose file tools
The moose likes Beginning Java and the fly likes What is String.intern ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply 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]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: What is String.intern ?
 
Similar Threads
pathnames
Regarding intern() method
canonical string representation
canonical class
canonical class