Introduction: We know that C + + has two parameter passing methods, value invocation and reference invocation. Some programmers think that the Java programming language uses reference calls to objects, which in fact are not.
Because of the universality of this misunderstanding, the question is described below.
So use a simple code to illustrate the problem:
1 //If the method parameter is a reference to the input, then we will exchange two pointers (that is, references)2 Public Static voidSwap (employee x, employee y)3 {4Employee temp =x;5x =y;6y =temp;7 }8 9 TenEmployee A =NewEmployee ("Alice", 5000); OneEmployee B =NewEmployee ("Trcy", 5000); A - -Swap (A, b);//In fact, there is only one pass-through value Pass in Java, except that the copy is a reference, and the copy version of the reference is still pointing to the original object.
Summary: After the exchange we expect the result should be, a refers to Trcy, b refers to Alice, but the result is not
1 Alice 2 Trcy
Print the result, a still references Alice, B still references Trcy. It shows that swap is closely swapping two copies.
With the end of the swap method call, the memory of the two copies is freed, and the swap does no work;
From Java Core technology:
1. A method cannot modify a parameter of a basic data parameter type (built-in type, Boolean value)
2. A method can modify the state of an object parameter.
3. A method cannot modify an object parameter to refer to a new object.
Getting Started with Java Learning: Method parameter transfer (function transfer problem)