|
JAVA
Pass by Reference
Rodelio
P. Barcenas
The
example program belows shows how a pass by reference is executed
whenever an array
is passed to a method.
public
class byReference{
public
static void main(String[] arg){
int[]
a;
a = new int[3];
a[0] = 1;
a[1] = 2;
a[2] = 3;
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);
change(a);
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);
}
public
void change(int[] x){
x[0]
= 3;
x[1] = 2;
x[0] = 1;
}
}
NOTE:
When the array is passed to the change method, the value is not
passed but only
the location in the memory where it reside.
If an array is passed to a method, a manipulation to the passed
array will result to a change
in the original array. In pass by value, the original value remains
unchanged.
|