This question is based on Scope of Object chapter of Thinking in Java, 2nd edition, page 109 and it says that when we create a Java object using new it hangs around pass the end of scope. It gives this example:
{
String s = new String("a string");
} /* end of scope */
Then it says,
the reference s vanishes at the end of the scope. However, the String object that s was pointing to is still occupying memory. In this bit of code, there is no way to access the object because the only reference to it is out of scope.
So if I understand correctly, the text "a string" is still there in memory, but the pointer which has the memory address of the first character, 'a', does not exist. Is this what it meant?
It goes on to say,
It turns out that because objects created with new stay around for as long as you want them, a whole slew of C++ programming problems simply vanish in Java.
Why would this be advantageous? In the above example the string data continues to reside in memory with no way to access it (since the pointer was destroyed after it went out of scope ) which is only eating up resources.