The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

String explained !!!

Posted by Kishori Sharan on December 01, 2000 at 10:12 AM

The "String" class maintains a pool of strings internally for your java program.
So, your next question will be "what is the content of this pool?". Whenever, you
initialize an object of class "String" with a constant expression then the value of that
constant expression ( of course, it will be a String ) is stored in the "String" pool and
in Java jargon we say that the string has been interned. Let us take an example.
String str1 = "hello" ;
String str2 = "hi" ;
String str3 = "hello" + " hi" ;
Now you can see all the three String objects str1, str2 and str3 have been
initialized with a constant expression and so the global ( but private ) pool maintained by
class String has three entries viz. "hello", "hi" and "hello hi" . Now you may initialize a fouth
String object like
String str4 = "hello" + str2 ;
This time the variable str4 has been assigned a value "hellohi", but this
string "hellohi" is not stored in the pool of String class. Do you know why? Because,
the expression "hello" + str2 is not a constant expression. str2 is a variable.
I think you are clear about the "how part" of automatic intering of a string in java.
Let us move to another question "why part" - "Why String class interns all the stings resulting
from a constant expression?".
Write one more line of code as follows.
String str5 = "hello" ;
You know str1 and str5 are two different objects of class String. What is common
with them. Of course, the content which is "hello". No. We are partially right in saying
str1 and str5 have only one thing in common and that is their content "hello". They are exactly
representing the exactly same object in memory. But, how all these rubbish happened?
Here comes the importance of automatic interning the strings. Whenever, you wrote a code
like
String temp = "hello" ;
first of all java looks for the string "hello" in the internal string pool
of class String. If it has already interned a String "hello" then it just returns the
reference to that string "hello" in pool to the variable "temp", otherwise it interns
the string and then returns the reference. Since in our case before we wrote
String str5 = "hello" ;
the string "hello" was already interned by statement
String str1 = "hello" ;
So now both str1, str5 and temp are pointing to the same string object
in string pool whose content is "hello". But, how to prove it? You know the == operator
in java compares the references of two objects. Now you can prove it like

if ( str1 == str5 ) {
System.out.println ( "Kishori din't lie about string intering" ) ;
}
else {
System.out.println ( "Kishori lied about string intering" ) ;
}

This way java utilizes the memory efficiently by
interning the strings. However, if you create a string object using new operator then
irrespective of the string content java always allocates new memory for each object.
String s1 = new ( "hello" ) ;
String s2 = new ( "hello" ) ;
Try s1 == s2 and it will return false because event if string contents are
same for s1 and s2 they have different references in memory. So in case of intering of strins
s1 == s2 a well as s1.equals ( s2 ) returns true where as in using new operator
s1 == s2 will never return true but s1.equals ( s2 ) may return true if and only if the
contents of two strings are same.
Last words. You can also intern a string, I mean you can store them
in string pool by calling s1.intern ( ) . If s1 is already ( in fact the content of s1 )
interned the it returns the reference of already interned string else interns this string and returns the
new reference.


Thanx
Kishori



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us