String alpha = new String("Red Rose") ; alpha = null; . . .
The first statement does two things:
(1) a String
object is created,
containing the characters "Red Rose".
Then, (2) a reference
to that object is saved in the reference variable alpha.
The second statement assigns the value null
to alpha
.
When this happens,
the reference to the object is lost.
Since there is no reference to the object elsewhere,
it is now garbage.
The line through the box in the second statement
symbolizes the null
value.
The object still exists in memory.
The memory it consists of will eventually be
recycled by
the garbage collector
and will be made available for new objects.
Examine this (slightly altered) snippet of code:
String alpha = new String("Red Rose");
String beta = alpha;
alpha = null;
. . .