Learning Java: Day 9

Constructors and the Garbage Collection
Have you ever run a software program that slowed down after you'd been working for a while, and it got so bad that you had to close the application and restart it in order to continue working?  That's what happens when the programmer doesn't pay close attention to getting rid of objects when those objects are done doing "its thing".  Those abandoned objects continue taking up RAM, so much so that eventually you run out of memory space and have to close the program in order to free the memory space up again.  Java automatically takes out the trash for you.

Chapter 8 of Heads Up Java covers how objects are created, where they live while they're alive, and how to keep or abandon them efficiently.

There are two areas of memory that are important in Java; the Stack and the Heap. 

The Heap is also known as "The Garbage Collectible Heap".  The Heap is where ALL, ALL, ALL objects live. The instance variables that belong to the object live inside their objects on the heap, too. 

The Stack is where method invocations (like go() and doStuff() and main()) along with local variables live. A local variable is alive until its method completes. A local variable is in scope only within the method in which the variable was declared.  You can only use that local variable while its method is actually running.  As soon as the method is completed, the variable is popped off the Stack and goes into garbage collection.

How Objects Are Created
  1. Declare a reference variable:                     Dog myPuppy = new Dog();
  2. Create an object                                      Dog myPuppy = new Dog();
  3. Link the object and the reference            Dog myPuppy = new Dog();

Constructors
A constructor looks and feels a lot like a method, but it's not a method.  It's got the code that runs when you say new. It's the code that runs when you instantiate an object. The thing that separates a method from a constructor is the return type. Methods must have a return type, but constructors cannot have a return type.

Overloaded constructors means you have more than one constructor in your class.  To compile, each constructor must have a different argument list.

How Long Does an Object Live?
An object's life depends exclusively on the life of the references referring to it.  If the reference is considered alive, the object is still alive on the Heap.  If the reference dies, the object will die and will be picked up by garbage collection.

3 ways to get rid of an object's reference:
  1. The reference goes out of scope, permanently 
    •  // the reference dies at the end of the method
  2. The reference is assigned another object       
    •  // the first object is abandoned when the reference moves
  3. The reference is explicitly set to null       
    •  // the first object is abandoned when the reference is 'deprogrammed'.
This chapter end with exercises, a puzzle and a five-minute mystery.

0 comments:

Post a Comment