×

Uh-oh, it looks like your Internet Explorer is out of date.

For a better shopping experience, please upgrade now.

Java 2 All-in-One Certification Exam Guide
     

Java 2 All-in-One Certification Exam Guide

3.6 5
by William R. Stanek, Barry Boone, Barry Boone (Other)
 
All-in-One is all you need! This authoritative reference offers complete coverage of all material on the Java 2 certification exams. You*ll find exam objectives at the beginning of each chapter,helpful exam tips,end-of-chapter practice questions,and photographs and illustrations. The bonus CD-ROM contains practice tests,hundreds of questions,and video clips. This

Overview

All-in-One is all you need! This authoritative reference offers complete coverage of all material on the Java 2 certification exams. You*ll find exam objectives at the beginning of each chapter,helpful exam tips,end-of-chapter practice questions,and photographs and illustrations. The bonus CD-ROM contains practice tests,hundreds of questions,and video clips. This comprehensive guide not only helps you pass this challenging certification exam,but will also serve as an invaluable on-the-job reference.

Prepare to pass both the programmer's and developer's certification exams Now you can prepare for the two widely taken Java 2 exams—the programmer's and developer's exams—using this single comprehensive volume. This reference will map out all objectives for both exams and provide you with Java programming essentials. Each chapter contains numerous practice questions,illustrations,and test-taking tips,making it a premier study tool. And,it's packed with helpful sidebars offering in-depth technical discussions of concepts and techniques you've just read about.

Get complete details on exam topics,including how to:

  • Apply programming fundamentals
  • Design application interfaces
  • Utilize graphics,components,and layout managers
  • Use objects,constructors,and classes
  • Find out about inheritance,multithreading,and string handling
  • Access and manage databases
  • Identify when an object is eligible for garbage collection
  • Work with control statements,data types,and variables

Product Details

ISBN-13:
9780072191691
Publisher:
McGraw-Hill Companies, The
Publication date:
09/25/2001
Edition description:
Third
Pages:
697
Product dimensions:
7.60(w) x 9.20(h) x 2.28(d)

Read an Excerpt

Chapter 5: Memory and Garbage Collection

Objectives for This Chapter

  • Identify when an object referred to by a local variable becomes eligible to be garbage collected (in the absence of compiler optimization).

  • Describe how finalization works and the behavior Java guarantees regarding finalization.

  • Distinguish between modifying variables containing primitive data types and object references, and modifying the objects themselves.

  • Identify the results of Java's pass-by-value approach when passing parameters to methods.
Java manages your program's memory. This eliminates all manner of bugs that creep into programs built with languages where you must access and manage memory directly. Java's role in managing memory eliminates bugs involved with the following:

  • Freeing memory too soon (resulting in a dangling pointer)

  • Not freeing memory soon enough or at all (resulting in a memory leak)

  • Accessing memory beyond the bounds of the allocated memory (resulting in using uninitialized values)
When you want to define a new data type, you define a new class. You can allocate an instance of a class by using the new keyword, which returns an object reference. This object reference is essentially a pointer to the object in memory—except in Java you cannot manipulate this object reference like a number. You cannot perform pointer arithmetic.

Objects are allocated from a garbage-collected heap. You cannot directly alter the memory in this heap. You can only get at the objects in this heap by using object refer-ences. Since Java manages the heap, and since you cannot manipulate object references, you must trust the Java Virtual Machine (JVM) to do what's right: free memory when it should, allocate the correct amount of memory when needed, and so on. But don't worry; the JVM is very good at its job—much better than any of us error-prone humans.

Garbage Collection

For the exam, you'll need to be able to identify when an object referred to by a local vari-able becomes eligible to be garbage collected (in the absence of compiler optimization). Since Java manages the garbage-collected heap, which is where all your objects live, you've got to trust Java to manage the memory for you. This includes believing that the JVM really will free memory that you no longer need when it runs low.

When Does an Object Become
Eligible for Garbage Collection?

An object becomes a candidate for garbage collection when your program can no longer reference it. Here is an example. Let's say you allocate an object and only assign it to a method variable. When that method returns, you have no way to ever refer to that method variable again. Therefore, the object is lost forever. That means that when the method returns, the object is a candidate for garbage collection.

As another example, you might create an object, assign this object to an object reference, and then set this object reference to null. As soon as you lose the reference to the object, that object becomes a candidate for garbage collection.

Just because your program might lose a reference to an object does not mean that the JVM will reclaim that object's memory right away, or even at all. The JVM will only perform garbage collection if it needs more memory to continue executing. For almost all simple programs, including the ones you've seen so far, the JVM doesn't even come close to running out of memory.


CAUTION: The reason the garbage collector does not reclaim memory as soon as it is available is that garbage collection takes time. If the garbage collector were continuously expunging allocated memory that you could no longer access, it would seriously slow your program's execution. It is not guaranteed that the garbage collector will reclaim objects in a certain order. This allows the garbage collector to run as efficiently as possible.

Even though a new keyword exists, notice that you do not explicitly indicate how much memory to set aside. The JVM determines this for itself. The JVM determines the memory your object requires based on

  • The amount of memory needed to maintain instance variables for the object

  • A standard overhead required by all objects
What's more, even though Java supplies the keyword new, no corresponding delete is available, nor any other way to directly free memory. The way you indicate you are through with an object is to set its object reference to null or to some other object. Or, as mentioned, when a method returns, its method variables will no longer be valid, so the garbage collector also knows any objects referenced by method variables are candidates for garbage collection.

Invoking the Garbage Collector

Even though you cannot free objects explicitly, you can directly invoke the garbage collector. This will make the garbage collector run, which will reclaim candidates for garbage collection.

They way you run the garbage collector is to perform two steps:

  1. Get an object that represents the current runtime.
  2. Invoke that object's gc() method.
Here is a snippet that does this:

Runtime rt = Runtime.getRuntime();
rt.gc();

Exercise 5-2 asks you to work through an example of this.

Finalization

For the exam, you'll need to know when finalization takes place. In most situations, you'll never know when garbage collection has occurred. Java runs the garbage-collecting process as a low-priority background thread. The garbage collector will run whenever memory gets low.

However, you can get into the act of garbage collection. If you want to perform some task when your object is about to go away, you can override a method called finalize().

Java will invoke the finalize() method exactly once for every object in your program. finalize() is declared as protected, does not return a value, and throws a Throwable object.

Java invokes the finalize() method just before an object is about to be garbage collected. You might take advantage of this notification to clean up any resources that have been allocated outside this object. A classic example is a file that an object has opened that might still be open. The object can check to see if the file has been closed in finalize() and, if not, it will close the file....

Meet the Author

William Stanek (Olympia, WA) produces practical and professional advice has helped millions of Web publishers, programmers, and developers all over the world. William is also a frequent contributor to PC Magazine.

Customer Reviews

Average Review:

Post to your social network

     

Most Helpful Customer Reviews

See all customer reviews

Java 2 All-in-One Certification Exam Guide 3.6 out of 5 based on 0 ratings. 5 reviews.
Guest More than 1 year ago
Don't bother reading the book, there are errors all over the place (begining with Ch 1). Guess if you don't have any knowledge of Java, you won't notice the errors! I wish I had read the book immediately after buying it. I read it couple of months after buying and so could not even return it for a refund.
Guest More than 1 year ago
After reading several reviews, i was disappointed when i finished reading this book .... The book contained syntax errors and codes that doesn't even compile. I wondered if the author ever compiled his codes .... I just finished the java 1.4 certification exam and passed with the help of "THINKING IN JAVA" by Bruce Eckel. ... I advise you to find the topic for the certification exam and then go purchase Thinking in java and review each topic. The book shoud provide you with explicit information to help you pass the exam ... *******Dont bother buying ANY certification exam books ***** You might as well spend the money on THINKING IN JAVA, which provide you detailed information rather generalize shorten reviews on each topic as given by certification books ... A few days before the exam you should sit for an hour or two at Barnes and Noble and review the sample exams given by certification books, but be advised that the author's mock exams may contain syntax and even semantics errors.... Well hope this helps... and if your still worrying about certification exams.... just know that i COUNTER STRIKE the week before the exam and studied at most 3 hours and still passed the exam.
Guest More than 1 year ago
I studied Java 2 from other books and bought this book so I could prepare for certification. This book is excellent for that purpose. The first 500 pages are about the programmer's exam. Each chapter is very comprehensive and in the end you can take practice tests about the whole area. This first part is what I use frequently to test my skills and knowledge.
Guest More than 1 year ago
This is the best Java book I found! I read several others but this was the one that answered my questions and was usable. I highly recommend this book to anyone looking to certify. This book also makes a great reference -- I used it everyday!
Guest More than 1 year ago
Java 2 AllinOne Cert guide is well-organized and very accurate! I've used it to help me learn more about Java programming and I refer to it often... I'm also using the book to study for certification. I originnaly picked this because it had the most thorough reviews. It has lots more exam review sections and questions than any other Java certification book. It is also the most complete in content. I HIGHLY RECOMMEND to anyone that is working with Java and/or studying with certification.