next up previous
Next: References to objects Up: Unit 02 Previous: Empty string

Accessibility of objects

Consider the following statements:

String s1 = new String ("test1");
String s2 = new String ("test2");
s1 = s2;

The references to s1 and s2 are initially two references to two newly created objects. The assignment statement sets the reference of s1 equal to the reference of s2 (two references to the same object "test2"), while the reference to the object "test1" created by the first statement is lost.

The operation of recovering the memory used by objects that have been "lost" by the program is called garbage collection. In Java, such an operation is performed automatically by the runtime system (i.e., the Java Virtual Machine).


next up previous
Next: References to objects Up: Unit 02 Previous: Empty string