1. Value transfer of basic data types (value types)
public class Client2 { public static void main(String[] args) { /************The mechanism of parameter passing in the test method************/ Client2 client2 = new Client2(); int a = 10; int b = 20; System.out.println("The value of i before calling the method: " + a); System.out.println("The value of j before calling the method: " + b); client2.fun(i,j); System.out.println("The value of i after calling the method: " + a); System.out.println("The value of j after calling the method: " + b); } void fun(int a,int b){ a += 5; b += 5; System.out.println("The value of a when calling the method: " + a); System.out.println("The value of b when the method is called: " + b); } }
The console prints the result:
The value of a before the method is called: 10
The value of b before the method is called: 20The value of a when the method is called: 15
The value of b when the method is called: 25The value of a after calling the method: 10
The value of b after calling the method: 20The values of a and b have not changed before and after the method is called, but the values of a and b have changed when the method is called, which verifies the passing of parameters. When the method is called, a copy of the parameters will be created. The original The value will not change.
Explain the passing process of parameters in the method through the memory analysis diagram
In the first step
, the main method main() of the program executes the two lines of code of the variable declaration from top to bottom during execution.
int a = 10;
int b = 20;
- 1
- 2
The changes in the memory space are shown in the figure:
- First, open up two spaces in the stack memory to store a and b respectively, and the address (hashcode) is null.
- Then, store the literals 10 and 20 in the constant pool, and generate their unique addresses 3308 and 3309 respectively (the addresses here are written casually, just for example, the real addresses are not the case).
- Finally, assign the address 3308 of the literal 10 and the address 3309 of the literal 20 to a and b respectively. Finally, the addresses corresponding to a and a are formed in the stack area as 3308 and the addresses 3309 corresponding to b and b.
The second step
executes the console print output statement
System.out.println("调用方法前a的值:" + a);
System.out.println("调用方法前b的值:" + b);
- 1
- 2
Print result: the value of a before calling the method: 10, the value of b before calling the method: 20.
The third step is
to execute the method fun1() and execute the console print output statement
ceshi.fun1(a,b);
- 1
System.out.println("调用方法时a的值:" + a);
System.out.println("调用方法时b的值:" + b);
- 1
- 2
Print result: the value of a when the method is called: 15, the value of b when the method is called: 25.
The changes in the memory space are shown in the figure:
- First, when the method is called, a copy a', b' of the parameters a and b are generated, and a space is created in the stack memory to store a', b', and the value addresses of a and b are passed to a', b'.
- Then, execute the method body statements a' = a' + 5 and b' = b' + 5 ie a' = 10 + 5 = 15 and b' = 20 + 5 = 25, and convert the newly generated literals 15 and 25 Store in the constant pool, and generate corresponding addresses 3311 and 3312 at the same time.
- Finally, assign new addresses 3311 and 3312 to a' and b' respectively, and form the addresses corresponding to a' and a' in the stack area as 3311 and b' and 3312 corresponding to b'.
The fourth step
executes the console print output statement
System.out.println("调用方法后a的值:" + a);
System.out.println("调用方法后b的值:" + b);
- 1
- 2
Print result: the value of a after calling the method: 10, the value of b after calling the method: 20.
2. Pass by value of reference data type (class type)
public class Client2 { public static void main(String[] args) { /***********测试方法中参数传递的机制*************/ Client2 client2 = new Client2(); int a = 10; int b = 20; P p = new P(); p.a = 10; p.b = 20; System.out.println("调用方法前a的值:" + p.a); System.out.println("调用方法前b的值:" + p.b); client2.fun(p); System.out.println("调用方法后a的值:" + p.a); System.out.println("调用方法后b的值:" + p.b); } void fun(P p) { p.a += 5; p.b += 5; System.out.println("调用方法时a的值:" + p.a); System.out.println("调用方法时b的值:" + p.b); } } class P { int i; int j; }
控制台打印结果:
调用方法前 a 的值:10
调用方法前 b 的值:20调用方法时 a 的值:15
调用方法时 b 的值:25调用方法后 a 的值:15
调用方法后 b 的值:25
如果方法中传入的是引用数据类型(即类类型),那么传入方法中,如果对对象的相关属性进行了修改,那么原对象的相关属性也会发生改变。
Explain the passing process of parameters in the method through the memory analysis diagram
In the first step
, the main method main() of the program executes the two lines of code of the variable declaration from top to bottom during execution.
int a = 10;
int b = 20;
- 1
- 2
The changes in the memory space are shown in the figure:
- First, open up two spaces in the stack memory to store a and b respectively, and the address (hashcode) is null.
- Then, store the literals 10 and 20 in the constant pool, and generate their unique addresses 3308 and 3309 respectively (the addresses here are written casually, just for example, the real addresses are not the case).
- Finally, assign the address 3308 of the literal 10 and the address 3309 of the literal 20 to a and b respectively, and finally form the addresses corresponding to a and a in the stack area as 3308 and the addresses corresponding to b and b 3309
The second step
executes the console print output statement
System.out.println("调用方法前a的值:" + a);
System.out.println("调用方法前b的值:" + b);
- 1
- 2
Print result: the value of a before calling the method: 10, the value of b before calling the method: 20.
The third step is
to execute the method fun1() and execute the console print output statement
ceshi.fun1(a,b);
- 1
System.out.println("调用方法时a的值:" + a);
System.out.println("调用方法时b的值:" + b);
- 1
- 2
Print result: the value of a when the method is called: 15, the value of b when the method is called: 25.
Changes in memory space:
- First, when calling the method, parameters a and b open up space in the heap memory to store the values of a and b, and pass the value addresses of a and b in the heap area to a and b in the stack area.
- Then, execute the method body statement a = a + 5 and b = b + 5 that is a = 10 + 5 = 15 and b = 20 + 5 = 25, and replace the original a in the heap area with the newly generated literals 15 and 25 , the value of b.
- The addresses of a and b in the stack area pointing to the values of a and b in the heap area remain unchanged. When the values of a and b are finally output, the values of a and b have been replaced, that is, a=15, b=25.
the fourth step
Execute the console printout statement
System.out.println("调用方法后a的值:" + a);
System.out.println("调用方法后b的值:" + b);
- 1
- 2
Print result: the value of a after calling the method: 15, the value of b after calling the method: 25.