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.