In Java, there is only one way to pass the parameter: Pass the value; this is different from C ++. c ++ has two methods: Pass the value and reference. Although Java only transmits the value
A transfer method, but the impact of a function on parameters varies according to the parameter type. It can be said that the form parameter is a copy of the real parameter,
When the parameter type is the basic data type, the processing of the form parameter does not affect the real parameter, that is, you want to exchange two real parameters through the form parameter is not feasible; when the form parameter
When it is an object (in fact, it is an object reference), the form parameter and the real parameter point to the same object, so operations on the object through the form parameter will affect the real Parameter
Object. Let's take a look at the following swaplist Function
// Exchange two List objects
Public static void swaplist (list <integer> Lista, list <integer> listb ){
List <integer> temp = lista;
Lista = listb;
Listb = temp;
}
Execute this function
List <integer> Lia = new arraylist <integer> ();
List <integer> Lib = new arraylist <integer> ();
Int x = 3;
Int y = 5;
Lia. Add (X );
Lib. Add (y );
Swaplist (LIA, Lib); // exchange
For (INT each: Lia)
System. Out. println (each );
The elements in the printed Lia are still 3, indicating that the exchange was not successful. If you think about it carefully, you will find that the swaplist function, Lista, listb
It is a copy of Lia and Lib, and lista is exchanged with listb. Lista points to the object pointed to by the original listb, and listb points to the object pointed to by the original lista.
Object, but the processing of lista and listb does not affect Lia and Lib. Therefore, Lia and Lib point to the original object respectively.
Finally, the python parameter passing method is basically the same as that of Java.