Java Reference
In-Depth Information
public static int
sign(
int
n) {
if
(n >
0
)
return
1
;
else if
(n ==
0
)
return
0
;
else if
(n <
0
)
return
-1
;
public static int
sign(
int
n) {
if
(n >
0
)
return
1
;
else if
(n ==
0
)
return
0
;
else
return
-1
;
Should be
}
}
(a)
(b)
To fix this problem, delete
if (n < 0)
in (a), so the compiler will see a
return
statement to be reached regardless of how the
if
statement is evaluated.
Note
Methods enable code sharing and reuse. The
max
method can be invoked from any
class, not just
TestMax
. If you create a new class, you can invoke the
max
method
using
ClassName.methodName
(i.e.,
TestMax.max
).
reusing method
Each time a method is invoked, the system creates an
activation record
(also called an
acti-
vation frame
) that stores parameters and variables for the method and places the activation
record in an area of memory known as a
call stack.
A call stack is also known as an
execution
stack
,
runtime stack
, or
machine stack
, and it is often shortened to just “the stack.” When a
method calls another method, the caller's activation record is kept intact, and a new activation
record is created for the new method called. When a method finishes its work and returns to
its caller, its activation record is removed from the call stack.
A call stack stores the activation records in a last-in, first-out fashion: The activation record
for the method that is invoked last is removed first from the stack. For example, suppose
method
m1
calls method
m2
, and
m2
calls method
m3
. The runtime system pushes
m1
's activa-
tion record into the stack, then
m2
's, and then
m3
's. After
m3
is finished, its activation record is
removed from the stack. After
m2
is finished, its activation record is removed from the stack.
After
m1
is finished, its activation record is removed from the stack.
Understanding call stacks helps you to comprehend how methods are invoked. The vari-
ables defined in the
main
method in Listing 6.1 are
i
,
j
, and
k
. The variables defined in
the
max
method are
num1
,
num2
, and
result
. The variables
num1
and
num2
are defined
in the method signature and are parameters of the
max
method. Their values are passed
through method invocation. Figure 6.3 illustrates the activation records for method calls in
the stack.
activation record
call stack
Activation record for
the
max
method
Activation record for
the
max
method
result: 5
num2: 2
num1: 5
result:
num2: 2
num1: 5
Activation record
for the
main
method
k:
j: 2
i: 5
Activation record for
the
main
method
Activation record for
the
main
method
Activation record
for the
main
method
k: 5
j: 2
i: 5
k:
j: 2
i: 5
k:
j: 2
i: 5
Stack is empty
(a) The
main
method is invoked.
(b) The
max
method is invoked.
(c) The
max
method
is being executed.
(d) The
max
method is
finished and the return
value is sent to
k
.
(e) The
main
method is finished.
F
IGURE
6.3
When the
max
method is invoked, the flow of control transfers to the
max
method. Once the
max
method is
finished, it returns control back to the caller.
Search WWH ::
Custom Search