1. Parameter passing of basic data type
1 classPassvalue2 {3 4 Public Static voidmain (String [] args)5 {6 intX=5;7 Change (x);8System.out.println ("The current value is" +x);9 }Ten Public Static voidChangeintx) One { AX=3; - } - } the /* - F:\java_example>java Passvalue - The current value is 5 - */
View Code
Argument x and Parameter x, although the names are the same, do not have the same memory space, and the scope is different. The scope of the parameter x is limited to the change (), and its value varies from first to 5 in main () to X, then to the value of X from 5 to 3 through change (), to the end of the call to change (), and to the memory of X (). In this series of processes, the value of x in the argument x, which is main (), has not changed, so the final print is 5
2. Parameter passing of reference data type
classpassvalue{intx; Public Static voidmain (String [] args) {Passvalue obj=NewPassvalue (); obj.x=5; Change (obj); System.out.println ("The current value is" +obj.x); } Public Static voidChange (Passvalue obj) {obj.x=3; }}/*F:\java_example>java passvaluethe Current value is 3*/
View Code
, the formal parameter, obj, is a new reference handle, but its scope is limited to the change function, but it has changed the value of x in the object before it is destroyed, so the print is 3.
Java Basics-Learning notes (eight)--parameter passing of functions