The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
July 2000



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.



Posted by Kishori Sharan on July 19, 2000 at 5:20 PM

Hi Sanjay

A String object can be created in many ways like

String s1 = "a literal" ;
String s2 = new String ( ) ;
String s3 = new String ( "Hello" ) ;

When you take the first approach i.e. String s1 = "a literal" to create an object of String class then java creates a new object of class String in class pool and assigns the reference of that new object to s1. Let's consider the following code
String ss1 = "hi" ;
String ss2 = "hi" ;

For ss1 the above rule is applied, that is, a new string object is created with value "hi" and reference is returned to ss1. But, for ss2 java again searches class pool looking for the literal "hi" and it finds it in the class pool. So this time java returns the same reference to ss2. So now ss1 and ss2 are two objects having same reference. This can be proved by the following line
if ( ss1 == ss2 )
print ( "ss1 and ss2 are equal" );
print ( "ss1 and ss2 are not equal" );

You will get the result "ss1 and ss2 are equal" . == operator compares the objects reference and not the values .
Here , ss1.equals ( ss2 ) will also return "true" because they have the same value "hi" in them.

When you create s String object
String s2 = new String ( );
then java allocates the memory for the new object on heap and returns the reference to s2. This time it also initializes s2 to an empty string. The approach
String s3 = new String ( "Hello" ) ; does the same this thing except it sets value "Hello" for s3.
In java you have "new" operator but not "delete" one. Deleting part is taken care of by Garbage Collector. When there is no reference pointing to an object in memory that memory is released when garbage colleactor runs. For local references (local to methods ) this happens when method call is over. However, you can also set the object reference to null indicating that this reference doesn't point to any location in memory. Like
String s1 = new String ( "Thanx" );
allocates memory on heap and s1 points to that memory. However, if you say
s1 = null ; later then it breaks the link of s1 and the memory on heap. So now s1 is not pointing to any memory on heap and this is tantamout to saying that s1 is null. In your first declaration you are saying
String s1 = null ; which is same saying s1 is not pointing to any valid memory on heap. Both of the following statement are same for class instance variables.
String s1 = null ;
String s2 ;
Class's instance object ( here s2 ) are initialized to null by default. However, for local object in methods you will get error for line "String s2;"

I think you got confused with
String s1 = null ;
String s2 = ""; Here null is a key word in java and it has special meaning as mentioned above where as String s2 = "" ; is simple object creattion .
So your NUllPOinterException error is a valid error.



Sponsored Links

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