The process of passing data to formal parameters when calling a method is called parameter passing. There are two modes of transfer in programming languages: transfer by value and transfer by reference. It must be emphasized that the two delivery methods mentioned here are not limited to the delivery methods used in java, but are the delivery methods that appear in many programming languages including java.
2. Variable type2. Value passing
2. Original variable
public void change(int b) { b = 7; } @Testpublic void testBasic() {int a = 9; change(a); System.out.println(a); }
When passing parameters, according to the rules of value passing, variable b receives a copy of a, which also points to the literal value "9":
##Next, inside the method, assign a value of 7 to b, so that b points to 7. Since a and b are two independent variables, there is no reference and reference relationship between them, and a still points to 9. :
##3.String
public void change(String str01) { str01 = "baidu"; } @Testpublic void testString() { String str = new String("www.baidu.com"); change(str); System.out.println(str); }
When passing parameters, str passes a copy of itself to str01, so that str01 also points to the heap storing "www.baidu.com" Object:
Assign a value to str01 inside the method, so that str01 points to "baidu" in the string constant pool in the method area ", str still points to "www.baidu.com" in the heap, str and str01 point to different objects, and do not affect each other:
One thing to note here: Java designs String as an immutable object, that is, once the literal value contained in the String object changes, Java will create a new object and point the variable to the new object.
4.StringBuilder
public void change(StringBuilder builder01) { builder01.append(" World!"); } @Testpublic void testStringBuilder() { StringBuilder builder = new StringBuilder("Hello"); change(builder); System.out.println(builder); }
After the parameter transfer is completed, the builder01 variable obtains a copy of the builder variable. The copy and the original variable point to the same object in the heap:
Inside the method, the variable builder does not point to a new object, but still points to the same object as builder, so when builder accesses the same object in the heap, the data changes:
5. Custom type
public class MyInner {public int a; }public class Test{public void change(MyInner in01) { in01.a = 1; } @Testpublic void testDemain() { MyInner in = new MyInner(); in.a = 9; change(in); System.out.println(in.a); } }
The execution process is the same as that of StringBuilder, so I won’t go into details here. Let’s make some changes to the above code, as follows:
public class MyInner {public int a; }public class Test{public void change(MyInner in01) { in01=new MyInner();//使in01指向一个新的对象in01.a = 1; } @Testpublic void testDemain() { MyInner in = new MyInner(); in.a = 9; change(in); System.out.println(in.a); } }
实际输出:9
参数传递完成时,in01与in指向同一个对象,in01对对象的操作等同于in对对象的操作,接着在方法内部执行"in01=new MyInner();",这样in01就指向了一个新的对象,in01所有的操作都与in无关了:
综合以上的运行结果与分析,可知java参数传递方式符合值传递。
将变量自身的内存地址传入方法中,方法中的变量指向方法外的变量,在方法中对变量的操作就是对方法外变量的操作。
public class MyInner {public int a; }public class Test{public void change(MyInner in01) { in01=new MyInner();//使in01指向一个新的对象in01.a = 1; } @Testpublic void testDemain() { MyInner in = new MyInner(); in.a = 9; change(in); System.out.println(in.a); } }
实际输出:9
如果采用引用传递,传递完成以后,in01指向in,对in01的操作就是对in的操作,in01指向对象2,那么in也指向对象2,输出1,与实际不符,所以不是采用引用传递。
不再一一分析其他变量类型,分析后可以发现,java在传递参数时采用的不是引用传递,而是值传递。
简单讲,值传递时方法内外是两个拥有同一指向的变量,引用传递时方法内外是同一个变量。
The above is the detailed content of What is parameter passing? What is his use?. For more information, please follow other related articles on the PHP Chinese website!