a good walk spoiled.
The pictures show what happens as the program runs. When the program starts there are two reference variables, but no objects. When the first statement executes,
String str = new String( "Golf is a good walk spoiled." ); // create the original object
it creates a String
object and puts a reference to that object
in the variable str
.
(The picture shows only the data part of the objects.)
When the second statement executes,
String sub = str.substring(8); //create a new object from the original
a new object is created that contains a substring of the characters
in the first object.
The variable sub
is assigned a reference to this object.
Does running the substring()
method of a String
object
change that String
object?