JAVA 是值传递,栈内值的传递
堆只是存储空间,栈代表线程执行过程,JAVA 方法参数传递只与栈有关
认真看下下方代码,就懂了
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
35
36
37
38
39
public class Test {
public void fun01() {
User user01 = new User();
user01.username = "张三";
fun02(user01);
System.out.println(user01);
User user02 = new User();
user02.username = "张三";
fun03(user02);
System.out.println(user02);
}
public void fun02(User user) {
user.username = "王五";
}
public void fun03(User user) {
User temp = new User();
temp.username = "王五";
user = temp;
}
public static void main(String[] args) {
Test test = new Test();
test.fun01();
}
class User {
String username;
@Override
public String toString() {
return "username=" + username;
}
}
}