// ArrayParam.java by Craig Persiko
// Demonstrates how array parameters work
// (reference varaibles are used for arrays)
class ArrayParam
{
public static void main(String[] args)
{
int[] a = new int[5];
int i = 7;
a[0] = 10;
fun(a, i);
System.out.println("done with fun");
System.out.println(a.length + " = length and a[0] = " + a[0]);
System.out.println("i = " + i);
}
static void fun(int[] aa, int ii)
{
aa[0] = 12;
// the above line changes one of the array elements that was
// allocated in main.
// Reference variables are passed by reference.
ii = 15;
// the above line changes ii only, not i:
// primitive variables are passed by value
// the below line makes "aa" refer to a new array
// (the main method's "a" is unaffected)
aa = new int[10];
aa[0] = 75;
}
}
/* Output:
done with fun
5 = length and a[0] = 12
i = 7
*/
syntax highlighted by Code2HTML, v. 0.9