public class SwapDemo { private int value; public SwapDemo(int val) { value = val; } public void setValue(int val) { value = val; } public int getValue() { return value; } public static void main(String[] args) { SwapDemo x = new SwapDemo(3); SwapDemo y = new SwapDemo(5); System.out.println("Before swap1, x is " + ` x.getValue() + "; y is " + y.getValue()); swap1(x, y); System.out.println("After swap1, x is " + x.getValue() + "; y is " + y.getValue()); System.out.println("Before swap2, x is " + x.getValue() + "; y is " + y.getValue()); swap2(x, y); System.out.println("After swap2, " + "x is " + x.getValue() + "; y is " + y.getValue()); int a = 3; int b = 5; System.out.println("Before int swap, a is " + a + "; b is " + b); swap(a, b); System.out.println("After int swap, a is " + a + "; b is " + b); } public static void swap1(SwapDemo x, SwapDemo y) { System.out.println("Begin swap1, x is " + x.getValue() + "; y is " + y.getValue()); SwapDemo temp; temp = x; x = y; y = temp; System.out.println("End swap1, x is " + x.getValue() + "; y is " + y.getValue()); } public static void swap2(SwapDemo x, SwapDemo y) { System.out.println("Begin swap2, x is " + x.getValue() + "; y is " + y.getValue()); int temp = x.getValue(); x.setValue(y.getValue()); y.setValue(temp); System.out.println("End swap2, x is " + x.getValue() + "; y is " + y.getValue()); } // this is identical to the C++ version; only println's added public static void swap(int x, int y) { System.out.println("Begin int swap, x is " + x + "; y is " + y); int temp = x; x = y; y = temp; System.out.println("End int swap, x is " + x + "; y is " + y); } }