This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Hi can anyone give me a really basic description of what the intern method of the string object does. Something to do with storing strings to a pool is as far as my understanding goes. Thanks in advance, Simon [ edited to change typo in subject -ds ] [ September 28, 2002: Message edited by: Dirk Schreckmann ]
Returns a canonical representation for the string object. A pool of strings, initially empty, is maintained privately by the class String. When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned. All literal strings and string-valued constant expressions are interned.
My Interpretation The String class is apparently taking some control of the creation of new String objects. If a client requests a new String, instead of just always creating a new String (as would commonly be done by most class definitions), this pool of Strings is checked for an already-existing String with the same value as the one requested. If such a String already exists, the existing String is used and a new String object is not created (and less memory is allocated since a new String object was not created, but an existing one was reused). Note that if Strings weren't immutable (i.e. unchangeable), then such a memory management strategy wouldn't work. Since Strings are immutable, multiple references can use (i.e. refer to) the same String object without worrying about some other reference changing that String. Why take such control of the creation of Strings? Well, Strings are used very frequently in programming. So, any improvements in speed or reduced memory requirements concerning Strings are likely to be noticed and appreciated. Notice that if for some reason, the client (user of a String) really wanted a reference to an original String object (one not in or added to the pool), then the String(String) constructor could be used. Pictures might help to describe the situation, but is this making some since so far?
The source code of the String class, which may help to explain some of the details of what is going on. In the base directory of the Java 2 SDK is likely a file named src.jar or src.zip. This file can be uncompressed (with many popular zipping tools such as WinZip) in order to view the source code for most all of the class files in Java 2.
JavaRanchers! So, if you have more questions, ask away.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Sep 28, 2002
Yeah i think i get it now, all interned strings are unique so memory is saved, and they are faster to compare because if the reference to their location in memory is the same then they are the same (i.e. no checking character by character) Good forum this. Thanks, Simon