// Parameter passing in Java: call-by-value only * 2003-03-12 // // Compile with: // javac Parameters.java // Run with: // java Parameters 11 22 class Parameters { public static void main(String[] args) { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); // After the call to swapV, variables a and b are unchanged System.out.println("a = " + a + " b = " + b); swapV(a, b); System.out.println("a = " + a + " b = " + b); System.out.println("----------------------------------------"); C aa = new C(a); C bb = new C(b); // After the call to swapV, variables aa and bb are unchanged, // but the f fields in the objects referred to have been changed System.out.println("aa = " + aa + " bb = " + bb); System.out.println("aa.f = " + aa.f + " bb.f = " + bb.f); swapF(aa, bb); System.out.println("aa = " + aa + " bb = " + bb); System.out.println("aa.f = " + aa.f + " bb.f = " + bb.f); } static void swapV(int x, int y) { int tmp = x; x = y; y = tmp; } static void swapF(C x, C y) { int tmp = x.f; x.f = y.f; y.f = tmp; } } class C { public int f; public C(int f) { this.f = f; } }