Activity 3.5.1.
Run the code below. Click on Show CodeLens to step through the code with the Next button.
true
) or a value of reference type (such as “Hello” or game1
).int
variable k
, which initially stores the value 5, and a method myMethod()
, which takes an int
parameter n
. In this case, when we invoke myMethod(k)
, k
’s value (5) is copied into n
and stored there during the method. Run the code below to trace through it. Notice that the original variable in main does not change its value because its value is copied into the parameter variable.public class PrimitiveCall
{
public static void myMethod(int n)
{ System.out.println("myMethod: n= " + n);
n = 100;
System.out.println("myMethod: n= " + n);
} // myMethod()
public static void main(String argv[])
{ int k = 5;
System.out.println("main: k= " + k);
myMethod(k);
System.out.println("main: k= " + k);
} // main()
} // PrimitiveCall
k
in main()
cannot be altered from inside the method. Thus, the output generated by PrimitiveCall
ismain: k= 5
myMethod: n= 5
myMethod: n= 100
main: k= 5
main()
, k
’s value is printed both before and after myMethod()
is called, but that its value remains unaffected even though n
’s value is changed within the method. This is because myMethod()
contains just a copy of k
’s value, not k
itself. Any changes to the copy within myMethod()
leave k
unaltered.boolean
or int
or double
, is passed to a method, a copy of the value is passed. That’s why its original value remains unchanged outside the method, even if the copy is changed inside the method.PrimitiveCall
(a) Just before calling myMethod(k) in main
. (b) Just before executing the body of myMethod()
. (c) Just after executing the body of myMethod()
. (d) After flow of control returns to main()
.String
or another class type like OneRowNim
, is passed to a method, a copy of the reference to the object itself is assigned to the parameter. For example, in the case of a String
parameter or a OneRowNim
parameter, the method would be given a reference to the object–that is, the address of the object. The object itself is not passed, because it would be too inefficient to copy the entire object with all its data and methods. However, because the object’s reference gives the object’s location in memory, the method will have access to the object and can make changes to the original object from within the method.ReferenceCall
class (Listing 3.5.4). In this case, myMethod()
takes a parameter g
of type OneRowNim
. Because a OneRowNim instance is an object, g
is a reference variable. So when myMethod(game)
is invoked in main()
, a reference to game
is passed to myMethod()
. Note that in myMethod()
, we use takeSticks(3)
to change the number of sticks of g
from 10 to 7 and that this change persists even after the method returns control to main()
. The reason is that during the method’s execution, both game
and g
refer to the exact same object.public class ReferenceCall
{
public static void myMethod(OneRowNim g)
{ System.out.print("myMethod: Number of sticks: ");
System.out.println(g.getSticks());
g.takeSticks(3);
System.out.print("myMethod: Number of sticks: ");
System.out.println(g.getSticks());
} // myMethod()
public static void main(String argv[])
{ OneRowNim game = new OneRowNim(10);
System.out.print("main: Number of sticks: ");
System.out.println(game.getSticks());
myMethod(game);
System.out.print("main: Number of sticks: ");
System.out.println(game.getSticks());
}// main()
} // ReferenceCall
ReferenceCall
would bemain: Number of sticks: 10
myMethod: Number of sticks: 10
myMethod: Number of sticks: 7
main: Number of sticks: 7
OneRowNim
object in ReferenceCall
(a) Just before calling myMethod(game)
. (b) Just before executing the body of myMethod()
. (c) Just after executing the body of myMethod()
. (d) After flow of control returns to main()
.