File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes String's intern method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Watch "String New topic
Author

String's intern method

Simon Hearn
Greenhorn

Joined: Sep 28, 2002
Posts: 2
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 ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Welcome to JavaRanch!
The API Documentation is a good place to look first for information such as what a class or method does. The documentation for String tells us that the intern method
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?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Good sources for more information on this topic include:
  • Section 3.10.5 of the Java Language Specification
  • 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.
  • Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    And http://mindprod.com/jglossinterned.html


    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
    Simon Hearn
    Greenhorn

    Joined: Sep 28, 2002
    Posts: 2
    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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: String's intern method
     
    Similar Threads
    intern() method
    what's intern() in strings
    How many Objects ?
    char[] security
    intern method