Java Reference
In-Depth Information
This program begins by declaring and initializing an integer variable called
x
with
the value
17
:
method
main
x
17
It then calls the method
doubleNumber
, passing
x
as a parameter. The value of
x
is used to initialize the parameter
number
as a local variable of the method called
doubleNumber
:
method
main
method
doubleNumber
x
17
number
17
The program then executes the statements inside of
doubleNumber
. First,
doubleNumber
prints the initial value of
number
(
17
). Then it doubles
number
:
method
main
method
doubleNumber
x
17
number
34
Notice that this has no effect on the variable
x
. The parameter called
number
is a
copy of
x
, so even though they started out the same, changing the value of
number
does not affect
x
. Next,
doubleNumber
reports the new value of
number
(
34
).
At this point,
doubleNumber
finishes executing and we return to
main
:
method
main
x
17
The next statement in the
main
method reports the value of
x
, which is
17
. Then it
declares and initializes a variable called
number
with the value
42
:
method
main
x
17
number 42
Search WWH ::
Custom Search