Recently there are two types of data in the system learning core Java,java, the first is the basic data type (numeric and Boolean), the second is the class type (that is, the object), at first I thought that the first type of parameter was passed by value, the second was passed by reference, but in the course of learning core Java , I found this to be wrong, first to throw the final conclusion: in Java, all the method parameters are passed by value!!! Here's an explanation.
The first type of argument is well understood by value invocation, mainly to illustrate that the second type of parameter is also called by value, see the following program:
1 Public Static void swap (A x, a Y) 2 {3 A temp = x; 4 x = y; 5 y = temp; 6 }
If it is passed by reference, then this method should be able to implement data exchange (a reference to Rose,b reference jack):
Newnew A ("Rose"); Swap (A, b);
In practice, however, the swap method does not change the references to A and B, and X and y of the swap parameter are initialized to a reference copy of A and B, which swaps references to X and Y, and ultimately, in vain, when the method ends, x and y are discarded, and A and B still refer to the previously referenced object. As shown in the following:
This procedure shows that: in Java, the object is not passed by reference, in fact, the object reference is a value pass.
How to pass method parameters in Java