Section 5 : Operators and Assignments
Determine the effect upon
objects and primitive values of passing variables into
methods and performing assignments or other modifying
operations in that method.
Call by value:
This method copies the value of an
argument into the formal parameter of the sub-routine.
Therefore, changes made to the parameter of the
sub-routine have no effect on the argument used to
call it. In Java when a simple type is passed, it is
passed by value.
Call by reference:
In this method, a reference to an
argument (not the value of the argument) is passed to
the parameter. Inside the sub-routine, this reference
is used to access the actual argument specified in the
call. So, the changes made to the parameter will
affect the argument used to call the sub-routine. When
you pass an object to a method, it is passed by
When an object reference is passed to a
method, the reference itself is passed by value.
However, since the value being passed refers to an
object, the copy of that value will still refer to the
same object that its corresponding argument does.
Objects as parameters:
One of the most common uses of object
parameters involve constructors. Frequently you will
want to construct a new object which is initially the
same as some existing object. To do this you must
define a constructor that takes an object of its class
as a parameter. You will also require this when you
want to initialize an object of a class with an object
of another class (possibly of super class).
section5-1-1 | section5-1-2 | section5-2 | section5-3 | section5-4
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11