Consider the following example.
String str = new String();
str = \"Hello\";
System.out.println(str); //Prints Hello
str = \"Help!\";
System.out.println(s
Though java tries to ignore it, str
is nothing more than a pointer. This means that when you first write str = "Hello";
, you create an object that str
points to. When you reassign str
by writing str = "Help!";
, a new object is created and the old "Hello"
object gets garbage collected whenever java feels like it.