str = new String( "You know my methods, Watson.!" );
Before the program runs, there is no object.
The new String
object is created as the program runs.
class StringDemo1 { public static void main ( String[] args ) { String str; str = new String( "Elementary, my dear Watson!" ); } }
The declaration String str
creates a reference variable,
but does not create a String
.
The variable str
can be used to refer to a String
when one is created.
The next statement is an assignment statement that creates an object
and puts a reference to the object in str
.
After the program stops running,
the String
object no longer exists.
Its memory is reclaimed by the computer system for other uses.
(Review: ) What are the two steps in an assignment statement?