-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwap.java
More file actions
34 lines (34 loc) · 1.09 KB
/
Swap.java
File metadata and controls
34 lines (34 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class Swap {
// swap method for int
public static void swap(int i, int j){
int temp = i;
i = j;
j = temp;
System.out.println("swap内(int):x=" + i + ", y=" + j);
}
// swap method for Integer
public static void swap(Integer i, Integer j){
Integer temp = i;
i = j;
j = temp;
System.out.println("swap内(integer):x=" + i + ", y=" + j);
}
// main method
public static void main(String[] args){
// test swap with int
int a = 10;
int b = 20;
System.out.println("swap.int:");
System.out.println("Before swap: a = " + a + ", b = " + b);
swap(a, b);
System.out.println("After swap: a = " + a + ", b = " + b);
// test swap with Integer
// 方法1:自动装箱(最简洁,推荐)
Integer x = 100;
Integer y = 200;
System.out.println("swap.Integer:");
System.out.println("Before swap: x = " + x + ", y = " + y);
swap(x, y);
System.out.println("After swap: x = " + x + ", y = " + y);
}
}