Not Garbage

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Answer:

String alpha =  new String("Red Rose");
String beta  = alpha;
alpha = null;
  1. When was an object instantiated?       In the first statement.
  2. What happenes in the second statement?       The reference to the object is copied to beta.
  3. What becomes of that object?       It remains "alive," because there is still a reference to it.

Not Garbage

object with several references to it

 

 

 

This time, the object does not become garbage. This is because in the second statement a reference to the object is saved in a second variable, beta. Now when, in the third statement, alpha is set to null, there is still a reference to the object.

There can be many copies of the reference to an object. Only when there is no reference to an object anywhere does the object become garbage.

QUESTION 8:

(Review:) How can you determine what an object of a particular class can do?