// program to demonstrate the difference between value semantics (primitives) // and reference semantics (arrays) public class Semantics { public static void main(String[] args) { int x = 3; int y = 5; System.out.println("before swap: x = " + x + ", y = " + y); swap(x, y); System.out.println("after swap: x = " + x + ", y = " + y); System.out.println(); System.out.println(); int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; System.out.println("before swap: nums[x] = " + nums[x] + ", nums[y] = " + nums[y]); swapArray(nums, x, y); System.out.println("after swap: nums[x] = " + nums[x] + ", nums[y] = " + nums[y]); System.out.println(); System.out.println(); } // swaps x and y I hope! public static void swap(int x, int y) { int temp = x; x = y; y = temp; System.out.println("in swap: x = " + x + ", y = " + y); } // swaps a[x] and a[y] I hope! public static void swapArray(int[] a, int x, int y) { int temp = a[x]; a[x] = a[y]; a[y] = temp; System.out.println("in swap: a[x] = " + a[x] + ", a[y] = " + a[y]); } }