1. The basic type is used as a parameter,
When a method parameter is of the basic type, it is called by passing values.
(1) The value of the passed real parameter is a copy.
(2) One-way transmission. The parameter value can be modified inside the method, but such modification does not affect the corresponding real parameters.
Intuitively, the value passing process is equivalent to a value assignment process. The real parameters are the right values and the left parameters. They are associated only at the time of the call, and there is no association between them in the future.
2 composite type as parameter
If the real parameter is a class object, the system will pass the address value of the object to the form parameter when calling the corresponding method.
Although there is no pointer in Java, programmers do not need to master it, but there are still pointers in the system.
Because c ++ provides both value transfer and application call, some programmers also think that ijava objects use reference transfer, which is a misunderstanding. Java uses the call method of passing address values. In some cases, although the call effect is the same as that of the reference call, the difference between the two can be seen in other cases.
For example
Class onlytest {
Private int x = 0;
Public void setx (int ix ){
X = IX;
}
Public int getx (){
Return X;
}
}
Public class booleantest {
Public void swap (onlytest one, onlytest two ){
Onlytest temp;
Temp = one;
One = two;
Two = temp;
}
Public void showdiffer (){
Onlytest ox = new onlytest ();
Onlytest Oy = new onlytest ();
Ox. setx (100 );
Oy. setx (200 );
System. Out. println ("value before swap method call ");
System. Out. println ("Ox. x =" + ox. getx () + ", oy. x =" + oy. getx ());
Swap (OX, Oy );
System. Out. println ("value after swap method call ");
System. Out. println ("Ox. x =" + ox. getx () + ", oy. x =" + oy. getx ());
}
Public static void main (string ARGs []) {
Booleantest Va = new booleantest ();
Va. showdiffer ();
}
}
In method swap (), the form parameter is two onlytest objects. If it is a reference call, exchanging the values of the two objects will affect the real parameters ox and oy, the output result is as follows:
Value before the swap method is called
Ox. x = 100, oy. x = 200
Value after the swap method is called
Ox. x = 100, oy. x = 200
Java uses the address value transfer method. Therefore, when passing parameters, the address value is transmitted from ox to one, And oy to two. in this case, if ox and one point to the same region in the memory, oy and two point to the same region. In the exchange, the addresses of one and two are converted, But Ox and oy are not changed.
In the memory, the reference type is stored in the stack, while the new stuff is stored in the stack, and the stack stuff points to the stack. In this case, because one, and two are both stored in the stack, Ox and Oy pass the value to them, and there is no connection with them, so what they exchange is their own value, after the method is executed, the two values automatically disappear, so the values of ox and XY remain unchanged, mainly to figure out the memory.