/** * Swaps the values of two variables. * The first group of statement are incorrect. * The second group is the proper way to swap. * *@Author Anthony Larrain *@Version 1 Summer 2000 */ class Variable3{ public static void main(String [] args){ int first = 10; int second = 20; // output variables System.out.println("First is:\t" + first); System.out.println("Second is:\t" + second); // 1 first = second; second = first; System.out.println(); // blank line System.out.println("First attempt to swap"); System.out.println("First is:\t" + first); System.out.println("Second is:\t" + second); // reset the variable first back to the original value first = 10; // 2 // need a third variable to swap int temp = first; // now we can replace first because we have a copy of it. first = second; // need to give second the value initially in first second = temp; // output System.out.println(); // blank line System.out.println("Second attempt to swap"); System.out.println("First is:\t" + first); System.out.println("Second is:\t" + second); } } /* Notes: * \t implies a tab space * the statement first = second; take the value in the variable second * and copy it into the variable identitified by first. No change occurs * to second */