/************************************************************************ * Matt Kretchmar * March 22, 2006 * ParameterTest.java * This program illustrates the difference between pass-by-value * and pass-by-reference. Primitive types are always passed by value * while object types (including arrays) are always passed by * reference. ***********************************************************************/ class ParameterTest { public static void main ( String args[] ) { int x = 0; int a[] = {0, 0, 0}; System.out.println("Before method call, x = " + x ); primativeParameter(x); System.out.println("After method call, x = " + x ); System.out.println("Before method call, a[0] = " + a[0] ); objectParameter(a); System.out.println("After method call, a[0] = " + a[0] ); } /****************************************************************** * primativeParameter * This method illustrates the pass-by-value concept of primative * parameters. ******************************************************************/ public static void primativeParameter ( int x ) { // Changes to x are local only because this method keeps // a separate local copy of x. x = 3; } /****************************************************************** * objectParameter * This method illustrates the pass-by-reference concept of object * parameters. ******************************************************************/ public static void objectParameter ( int array[] ) { // Because the reference is passed in, changes to objects // (including arrays) will affect the actual parameter passed in. array[0] = 1; } }