范例一

class Demo{
	public int temp = 30;
}
public class T {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Demo d1 = new Demo();
		d1.temp = 50;
		System.out.println("fun()方法调用之前:"+d1.temp);
		fun(d1);
		System.out.println("fun()方法调用之后:"+d1.temp);
	}
	public static void fun(Demo d2){
		d2.temp = 1000;
	}
}


内存分析:

java学习笔记15--引用传递

范例二

public class T {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str1 = "hello";
		System.out.println("fun()方法调用之前:"+str1);
		fun(str1);
		System.out.println("fun()方法调用之前:"+str1);
	}
	public static void fun(String s2){
		s2 = "MLDN";
	}

}


内存分析

java学习笔记15--引用传递

范例三

class Demo{
	String temp = "hello";
}
public class T {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Demo d1 = new Demo();
		d1.temp = "world";
		System.out.println("fun()方法调用之前:"+d1.temp);
		fun(d1);
		System.out.println("fun()方法调用之后:"+d1.temp);
	}
	public static void fun(Demo d2){
		d2.temp = "MLDN";
	}
}


内存分析

java学习笔记15--引用传递

本程序与范例一的流程完全是一样的,范例二是特殊的,因为String是一个特殊的类,其内容不可改变

相关文章:

  • 2021-04-13
  • 2022-01-11
  • 2021-09-06
  • 2021-06-12
  • 2021-11-16
  • 2022-12-23
猜你喜欢
  • 2022-02-22
  • 2021-09-01
  • 2019-03-29
  • 2022-12-23
  • 2021-08-08
  • 2021-10-29
  • 2021-12-23
相关资源
相似解决方案