a good walk spoiled.
Here is a picture of what happens as the program runs. When the program first starts there are two reference variables, but they refer to 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 copy of a substring of the characters
in the first object.
A reference to this new object is assigned to the variable sub
.
Does running the substring()
method of a String
change that String
?