Passing Java function parameters

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.