Modifying the return value of a function in java may also affect the actual parameters (passing by reference and passing by value)

Value transfer ( the value of the actual parameter is copied and passed to the formal parameter)

Pass by reference ( the memory address of the actual parameter is copied and passed to the formal parameter)

Case 1

public double[] calculate(double[] parameter) {
		System.out.println("*************这是函数内部*************");
		System.out.print("形参内存地址" + System.identityHashCode(parameter) + " ");
		System.out.println("形参数据" + "[" + parameter[0] + ", " + parameter[1] + ", " + parameter[2] + "]");
		//使用了new关键字,说明变量box指向了新的内存地址
		double[] box = new double[parameter.length];
		for (int index = 0; index < parameter.length; index++) {
			box[index] = parameter[index];
		}
		System.out.println("..................box修改前..................");
		System.out.println("box内存地址" + System.identityHashCode(box));
		System.out.println("box数组的值为" + "[" + box[0] + ", " + box[1] + ", " + box[2] + "]");
		box[0] = 88.88;
		System.out.println("..................box修改后..................");
		System.out.println("box内存地址" + System.identityHashCode(box));
		System.out.println("box数组的值为" + "[" + box[0] + ", " + box[1] + ", " + box[2] + "]");
		System.out.print("形参内存地址" + System.identityHashCode(parameter) + " ");
		System.out.println("形参数据" + "[" + parameter[0] + ", " + parameter[1] + ", " + parameter[2] + "]");
		System.out.println("*************这是函数内部*************");
		return box;
	}

public static void main(String[] args) {
		User user = new User();
		
		double[] numbers = {5.7, 3.9, 22.78};
		System.out.print("调用函数前,实参内存地址" + System.identityHashCode(numbers) + " ");
		System.out.println("调用函数前,实参数据" + "[" + numbers[0] + ", " + numbers[1] + ", " + numbers[2] + "]\n");
		double[] res2 = user.calculate(numbers);
		System.out.println("\n调用函数后,函数返回值内存地址" + System.identityHashCode(res2));
		System.out.println("调用函数后,实参数据" + "[" + numbers[0] + ", " + numbers[1] + ", " + numbers[2] + "]");
		
		
	}

The results of the operation are as follows:

Case 2

The return value of the function is the memory address of the actual parameter

public static void main(String[] args) {
		Test2 test2 = new Test2();

		int[] socre = { 66, 88, 99 };
		System.out.println(socre[0]); // 66

		// 用变量result来接收函数的返回值(函数的返回值其实是实参的地址)
		int[] result = test2.t1(socre);

		System.out.println(socre[0]); // 66
		System.out.println(result[0]); // 66
        //修改函数的返回值,会影响到实参,说明函数的返回值和实参指向的是同一个内存地址
		result[0] = 27;

		System.out.println(socre[0]); // 27
		System.out.println(result[0]); // 27
		

	}

	public int[] t1(int[] p) {
		return p;// 这里返回的p,其实是实参的内存地址
	}

The results of the operation are as follows:

Case 3

public static void main(String[] args) {
		Test2 test2 = new Test2();

		int[] socre = { 66, 88, 99 };
		System.out.println(socre[0]); // 66

		// 用变量result来接收函数的返回值(函数的返回值其实是实参的地址)
		int[] result = test2.t2(socre);

		System.out.println(socre[0]); // 66
		System.out.println(result[0]); // 66
		//修改函数的返回值,会影响到实参,说明函数的返回值和实参指向的是同一个内存地址
		result[0] = 27;

		System.out.println(socre[0]); // 27
		System.out.println(result[0]); // 27
		

	}

	public int[] t2(int[] p) {
		int[] a = p;
		return a;
	}

The results of the operation are as follows:

Case 4

public static void main(String[] args) {
		Test2 test2 = new Test2();

		int[] socre = { 66, 88, 99 };
		System.out.println(socre[0]); // 66

		// 用变量result来接收函数的返回值(函数的返回值其实是实参的地址)
		int[] result = test2.t3(socre);

		System.out.println(socre[0]); // 23
		System.out.println(result[0]); // 23
		//修改函数的返回值,会影响到实参,说明函数的返回值和实参指向的是同一个内存地址
		result[0] = 27;

		System.out.println(socre[0]); // 27
		System.out.println(result[0]); // 27
		

	}

	public int[] t3(int[] p) {
		p[0] = 23;
		int[] a = p;
		return a;
	}

The results of the operation are as follows:

Case 5

public static void main(String[] args) {
		Test2 test2 = new Test2();

		int[] socre = { 66, 88, 99 };
		System.out.println(socre[0]); //

		// 用变量result来接收函数的返回值(函数的返回值其实是实参的地址)
		int[] result = test2.t4(socre);

		System.out.println(socre[0]); //
		System.out.println(result[0]); //
		//修改函数的返回值,会影响到实参,说明函数的返回值和实参指向的是同一个内存地址
		result[0] = 27;

		System.out.println(socre[0]); //
		System.out.println(result[0]); //
		

	}

	public int[] t4(int[] p) {
		int[] a = null;
		for (int i = 0; i < p.length; i++) {
			a[i] = p[i];
		}
		return a;
	}

The results of the operation are as follows:

Case 6

public static void main(String[] args) {
		Test2 test2 = new Test2();

		int[] socre = { 66, 88, 99 };
		System.out.println(socre[0]); //

		// 用变量result来接收函数的返回值(函数的返回值其实是实参的地址)
		int[] result = test2.t5(socre);

		System.out.println(socre[0]); //
		System.out.println(result[0]); //
		//修改函数的返回值,会影响到实参,说明函数的返回值和实参指向的是同一个内存地址
		result[0] = 27;

		System.out.println(socre[0]); //
		System.out.println(result[0]); //
	}

	public int[] t5(int[] p) {
		int[] a = null;
		p[0] = 78;
		for (int i = 0; i < p.length; i++) {
			a[i] = p[i];
		}
		return a;
	}

The results of the operation are as follows:

Case 7

public static void main(String[] args) {
		Test2 test2 = new Test2();

		int[] socre = { 66, 88, 99 };
		System.out.println(socre[0]); //66

		// 用变量result来接收函数的返回值(函数的返回值其实是实参的地址)
		int[] result = test2.t6(socre);

		System.out.println(socre[0]); //328
		System.out.println(result[0]); //66
		//修改函数的返回值,会影响到实参,说明函数的返回值和实参指向的是同一个内存地址
		result[0] = 27;

		System.out.println(socre[0]); //328
		System.out.println(result[0]); //27
	}

	public int[] t6(int[] p) {
		int[] a = new int[p.length];
		for (int i = 0; i < p.length; i++) {
			a[i] = p[i];
		}
		p[0] = 328;
		return a;
	}

The results of the operation are as follows:

Case 8

package com.test;

public class User {
	String hometown = "江西省赣州市于都县";
	int age = 26;

	public static void main(String[] args) {
		
	}
}
package com.test;

public class Test2 {
	public static void main(String[] args) {
		Test2 test2 = new Test2();
		
		User user = new User();

		System.out.println(user.hometown + "," + user.age); //江西省赣州市于都县,26

		// 用变量result来接收函数的返回值(函数的返回值其实是实参的地址)
		User result = test2.t7(user);

		System.out.println(user.hometown + "," + user.age); //江西省赣州市于都县,26
		System.out.println(result.hometown + "," + result.age); //江西省赣州市于都县,26
		//修改函数的返回值,会影响到实参,说明函数的返回值和实参指向的是同一个内存地址
		result.hometown = "赣州于都县";

		System.out.println(user.hometown); //赣州于都县
		System.out.println(result.hometown); //赣州于都县
	}

	public User t7(User u) {
		return u;
	}

}

The results of the operation are as follows:

Case 9

package com.test;

public class User {
	String hometown = "江西省赣州市于都县";
	int age = 26;

	public static void main(String[] args) {
		
	}
}
package com.test;

public class Test2 {
	public static void main(String[] args) {
		Test2 test2 = new Test2();
		
		User user = new User();

		System.out.println(user.hometown + "," + user.age); //江西省赣州市于都县,26

		// 用变量result来接收函数的返回值(函数的返回值其实是实参的地址)
		User result = test2.t8(user);

		System.out.println(user.hometown + "," + user.age); //我老家在江西于都县哦...,26
		System.out.println(result.hometown + "," + result.age); //我老家在江西于都县哦...,26
		//修改函数的返回值,会影响到实参,说明函数的返回值和实参指向的是同一个内存地址
		result.hometown = "赣州于都县";

		System.out.println(user.hometown); //赣州于都县
		System.out.println(result.hometown); //赣州于都县
	}

	public User t8(User u) {
		u.hometown = "我老家在江西于都县哦...";
		return u;
	}

}

The results of the operation are as follows:

Case 10

package com.test;

public class User {
	String hometown = "江西省赣州市于都县";
	int age = 26;

	public static void main(String[] args) {
		
	}
}
package com.test;

public class Test2 {
	public static void main(String[] args) {
		Test2 test2 = new Test2();
		
		User user = new User();

		System.out.println(user.hometown + "," + user.age); //江西省赣州市于都县,26

		// 用变量result来接收函数的返回值(函数的返回值其实是实参的地址)
		User result = test2.t9(user);

		System.out.println(user.hometown + "," + user.age); //江西省赣州市于都县,26
		System.out.println(result.hometown + "," + result.age); //江西省赣州市于都县,26
		//修改函数的返回值,会影响到实参,说明函数的返回值和实参指向的是同一个内存地址
		result.hometown = "赣州于都县";

		System.out.println(user.hometown); //赣州于都县
		System.out.println(result.hometown); //赣州于都县
	}

	public User t9(User u) {
		User u2 = u;
		return u2;
	}
}

The results of the operation are as follows:

Guess you like

Origin blog.csdn.net/czh500/article/details/114891005