The code below shows an example of:
- passing an array object into a method using a reference to
the array object,
- changing the array contents and
- passing the number of changes that were made back to the
main as a value.
public class PassByReference
{
int change(int arg[])
{
int cnt = 0;
for (int i=0; i < arg.length; i++)
{
if (arg[i] == 1)
{
cnt++;
arg[i] = 0;
}
}
return cnt;
}
public static void main(String[] args)
{
int array[] = {1,2,1,5,1,1,5};
int numChangesNeeded;
PassByReference pbr = new PassByReference();
System.out.print("Initial values in the array: [ ");
for (int i = 0; i < array.length; i++)
{
System.out.print(array[i] + " ");
}
System.out.println("]");
numChangesNeeded = pbr.change(array);
System.out.println("Number of ones in the array = " +
numChangesNeeded);
System.out.print("New values of the array: [ ");
for (int i = 0; i < array.length; i++)
{
System.out.print(array[i] + " ");
}
System.out.println("]");
}
}
Explanation of above code:
1) Start at the public static void main. Note there are two
definitions. The first definition is the array and during the definition,
we are placing numbers in the array. In this case, they have to be numbers
because this is an integer array. Note! an array whether it is an integer
array or a string array is still an array and therefore an object and not
a primitive. Next, we define the integer variable "numChangesNeeded" in
order to tell us how many changes the array needs.
2) We instantiate an instance of the class PassByReference in order to
do work.
3) We then print out the contents of the array before it is changed in
the first for loop.
4) The next step is to pass the array reference to the method change
that is part of the PassByReference instance that is called in this case
"pbr".
5) While in the change method, the elements in the array are all
checked in sequence and if the value of any of the elements is equal to
the value "1", it is changed in the original array to the value of
zero.
6) Each time a change is made, the cnt variable is incremented by one
which records a change being made.
7) Once all the changes are made and the for loop in the change method
completes by knowing how many elements are in the array by the array
length which is a value that is part of the array object, it quits.
8) When the work is all done in the change method, the value count is
passed back to the calling method and in this case, that method is
main.
9) Main then sees the cnt variable from the change method as the main's
variable numChangesNeeded which is a primitive. The number of changes is
then displayed.
10) The final step is another for loop that walks through the array and
prints the values. Note that all the values that were 1 are now changed to
zero.
Below is the output from the program:
Initial values in the array: [ 1 2 1 5 1 1 5 ]
Number of ones in the array = 4
New values of the array: [ 0 2 0 5 0 0 5 ]
Conclusion:
If you pass an object by reference and do work on that object, expect
to see the result of that work reflected in the object when you next
reference that object.
Remember, objects are passed by reference and primitives are passed
by value. But, the reference is really a value the tells where the
intended object resides.
Note! The Java program shown in this technote and its output were
developed, generated and verified using IBM's WebSphere Application
Developer version 5.1.
|