Page 286 - Introduction to Programming with Java: A Problem Solving Approach
P. 286
252 Chapter 7 Object-Oriented Programming—Additional Details
The question is, how does the Java Virtual Machine (JVM) treat abandoned or inaccessible objects? Inaccessible objects can’t participate in the program, so there’s no need to keep them around. They become “garbage.” In fact, it would be bad to keep them around, because they can lead to clogging up the computer’s memory. A computer has a finite amount of memory, and each piece of garbage uses up some of that mem- ory. And that means less memory is available for new tasks. If garbage is allowed to accumulate unabated, it would eventually chew up all the free space in a computer’s memory (free space is the portion of memory that is unused). If there’s no free space in memory, there’s no space for any new objects, and the computer stops working (until a reboot).
If an inaccessible object is allowed to persist and use up space in a computer’s memory, that’s called a memory leak. Memory leaks can occur in computer programs that allocate memory during execution. When a computer language requires the programmer to do something specific to prevent memory leaks, and the programmer forgets to do that, a nasty bug is born—a bug that is very hard to find. In creating the Java language, James Gosling and the good folks at Sun realized this, and they opted to make the language itself deal with the problem. How? By going into the garbage collection business. Not what Dirk and Lenny do when they pick up the trash at your curb every Tuesday, but Java garbage collection! Actually, James Gos- ling didn’t invent garbage collection; it’s been around since the dawn of garbage. But Java is the first popular programming language to include it as a standard service.
So what in the heck is garbage collection? It’s when a garbage collection program searches for inac- cessible objects and recycles the space they occupy by asking the operating system to designate their space in memory as free space. This space might not be used right away, and some computer whiz kid might be able to find some of those old abandoned objects—like wandering through a trash dump, fighting off mean dogs, and looking for furniture—but for practical purposes, you should consider those abandoned objects unrecoverable and gone. Apago PDF Enhancer
The beauty of Java’s automatic garbage collection is that the programmer doesn’t have to worry about it—it just happens whenever it’s appropriate. And when is it appropriate? Whenever the computer is running low on free space in memory or whenever nothing else is happening, such as when a program is waiting for keyboard input. At that point, the operating system wakes up his buddy the Java garbage collector, and tells him to go earn his keep.
7.4 Testing Objects for Equality
The previous section illustrated returning a reference from a method. This section illustrates passing a refer- ence to a method to allow the method to read the referenced object’s data. One of the most common applica- tions of this occurs in testing two objects for equality. Before looking at this application, it’s appropriate to look at the simplest way to evaluate equality.
The == Operator
The == operator works the same for primitive variables and for reference variables. It tests if the values stored in these variables are the same. When applied to reference variables, the == operator returns true if and only if the two reference variables refer to the same object; that is, the two reference variables contain the same address and thus are aliases for the same object. For example, what does the following code frag- ment print?
Car car1 = new Car();
Car car2 = car1;