2.10、深入引用(重点)
利用三道引用传递的范例,进一步巩固对引用传递的基本应用。
2.10.1、范例一
class Demo{
int x = 10;
}
public class Demo00 {
public static void main(String args[]) {
Demo d = new Demo();
d.x = 30;
fun(d);
System.out.println(d.x);
}
public static void fun(Demo temp){
temp.x = 100;
}
};
此程序的运行结果是100,下面分析内存的操作图:

2.10.2、范例二
public class Demo00 {
public static void main(String args[]) {
String str = "hello";
fun(str);
System.out.println(str);
}
public static void fun(String temp){
temp = "world";
}
};

2.10.3、范例三
class Demo{
String x = "mldn";
}
public class Demo00 {
public static void main(String args[]) {
Demo d = new Demo();
d.x= "hello";
fun(d);
System.out.println(d.x);
}
public static void fun(Demo temp){
temp.x = "world";
}
};

转载请注明来源