Java Reference
In-Depth Information
No objects are freed after the fi rst call to
gc
because no objects are eligible at that time.
After the second call to
gc
, the
Snoopy
object is eligible, but the call to
finalize
happens in
the thread of the garbage collector, so the output of
Snoopy
's
finalize
method may or may
not appear before the third call to
gc
. The exact output of running the
GCDemo3
program is
indeterminate. The previous output is just one possible result.
The
finalize
Method Is Only Invoked Once
Expect at least one question on the exam about the
finalize
method.
Keep in mind
that it can only be called once on an object, and it only gets called by the garbage
collector after an object is eligible for garbage collection but before the object is
actually garbage collected.
This ends our discussion on garbage collection, an important topic not just for the SCJP
exam but in our everyday programming of Java. Now we discuss another important topic
in Java: the concept of call by value.
Call by Value
The exam objectives state that you need to know “the effect upon object references and
primitive values when they are passed into methods that perform assignments or other
modifying operations on the parameters.” A variable that is passed into a method is called
an
argument
. Java simplifi es the concept of passing arguments into methods by providing
only one way to pass arguments: by value. Passing arguments
by value
means that a copy
of the argument is passed to the method. Method return values are also returned by value,
meaning a copy of the variable is returned. The SCJP exam requires an understanding of
what call by value means, and we will discuss the details now.
An argument is passed into a corresponding method parameter. A
parameter
is the
name of the variable in the method signature that gets assigned the value of the argument.
Let's look at an example. Suppose we have the following method defi nition:
21. public long cubic(int y) {
22. long longValue = (long) y;
23. y = -1;
24. return longValue * longValue * longValue;
25. }
Search WWH ::
Custom Search