The unique method for passing Java parameters: pass value (by value) (csdn)

Source: Internet
Author: User

The reason for this problem is that all the objects we operate on in Java are referenced by object references, which can be understood as pointers, it is easy for people to correspond to the C ++ address. The discussion is beneficial.

It is not confusing to directly pass the value when passing the basic data type variable. However, when passing the object, it should be understood as passing the value referenced by the object, but this value is not the object itself, but the object, that is, you let another reference point to the object.

-- Java transmits the object reference by value.

The following simple program is used as an example:

Package test;

Public class blogtest
{
Public void modifyaaa (int x, java. util. arraylist List)
{
X + = 5;
System. Out. println ("x =" + x );
List. Add (New java. util. Date ());
System. Out. println (list. Size ());
}

Public static void main (string [] ARGs)
{
Blogtest = new blogtest ();
Int I = 5;
Java. util. arraylist list = new java. util. arraylist ();
System. Out. println ("I ======" + I );
System. Out. println ("list. Size () =" + list. Size ());
Blogtest. modifyaaa (I, list );
System. Out. println ("I = new =" + I );
System. Out. println ("list. Size () =" + list. Size ());
}
}
/*
The running result is as follows:
I ======= 5
List. Size () = 0
X = 10
1
I = new = 5
List. Size () = new = 1
*/

This indicates that the following occurs during parameter transmission: int variable I is copied to int X, in modifyaaa () changing the value of X in the method does not affect the value of I in the main function. Of course, this is the variable survival interval, which is quite understandable. However, it is found that the list object is referenced before and after being passed to the modifyaaa () method, and the list in the main function also changes. This means that when the object reference is passed, the form parameter gets the reference copy of the real parameter object, both of which point to the same list object, so the form parameter changes the list and can also be reflected in the real parameter.

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.