public class ShowCode { public static void main(String[] args) { int a = 1; int b = 2; swap(a, b); System.out.println("print in main, a = " + a + ", b = " + b); }
private static void swap(int a, int b) { int temp = a; a = b; b = temp; System.out.println("print in swap, a = " + a + ", b = " + b); } }
代码的执行结果:
1 2
print in swap, a = 2, b = 1 print in main, a = 1, b = 2
public class PassMain { public static void main(String[] args) { Balloon red = new Balloon("Red Balloon", "Red"); Balloon blue = new Balloon("Blue Balloon", "Blue"); Balloon yellow = new Balloon("Yellow Balloon", "Yellow");
System.out.println("============= 1 ==============="); swap(red, blue); System.out.println("Red Balloon color is " + red.getColor()); System.out.println("Blue Balloon color is " + blue.getColor());
System.out.println("============= 2 ==============="); swap2(red, blue); System.out.println("Red Balloon color is " + red.getColor()); System.out.println("Blue Balloon color is " + blue.getColor());
System.out.println("============= 3 ==============="); paint(yellow); System.out.println("Yellow Balloon color is " + yellow.getColor()); }
============= 1 =============== Red Balloon color is Red Blue Balloon color is Blue ============= 2 =============== Red Balloon color is Blue Blue Balloon color is Red ============= 3 =============== Yellow Balloon color is Black