Package wit. test; </P> <p> public class Test2 <br/> {<br/> Public static void main (string [] ARGs) {<br/> // upload object <br/> aa a = new AA (); <br/>. n = 10; <br/> system. out. println ("before calling a function (the parameter is an object):" +. n); <br/> F (a); <br/> system. out. after println ("calling a function (the parameter is an object):" +. n); </P> <p> // transmits the basic type <br/> int n = 10; <br/> system. out. println ("before calling a function (the parameter is of the basic type INT):" + n); <br/> F (n); <br/> system. out. println ("after calling a function (the parameter is of the basic type INT):" + n); </P> <p> integer nn = new INTEGER (10 ); <br/> system. out. println ("before calling a function (the parameter is an integer of the basic type):" + nn); <br/> F (NN); <br/> system. out. println ("after calling a function (the parameter is of the basic type Integer):" + nn ); </P> <p> // string <br/> string S = new string ("ou"); <br/> system. out. println ("before calling a function (the parameter is string):" + S); <br/> F (s); <br/> system. out. println ("after calling a function (the parameter is string):" + S ); </P> <p> // passed stringbuffer <br/> stringbuffer sb = new stringbuffer ("Ouyang"); <br/> system. out. println ("before calling a function (the parameter is stringbuffer):" + Sb); <br/> F (SB); <br/> system. out. println ("after calling a function (the parameter is stringbuffer):" + Sb ); <br/> // string [] <br/> string [] Ss = new string [3]; <br/> SS [0] = ""; ss [1] = "yang"; <br/> system. out. println ("before calling a function (the parameter is string []):" + SS [0]); <br/> F (SS); <br/> system. out. println ("after calling a function (the parameter is string []):" + SS [0]); </P> <p> // The Int [] <br/> int [] II = {10, 20, 30} is passed. <br/> system. out. println ("before calling a function (the parameter is int []):" + II [0]); <br/> F (II); <br/> system. out. println ("after calling a function (the parameter is int []):" + II [0]); </P> <p >}< br/> // upload object <br/> Public static void F (aa a) {<br/>. n = 20; <br/>}< br/> // basic data transfer type <br/> Public static void F (int n) {<br/> n = 20; <br/>}< br/> Public static void F (integer N) {<br/> n = 20; <br/>}< br/> // string [] <br/> Public static void F (string []) {<br/> A [0] = ""; <br/>}< br/> // string <br/> Public static void F (string) {<br/> A = "ouyangping "; <br/>}< br/> // int [] <br/> Public static void F (int A []) {<br/> A [0] = 20; <br/>}< br/> // stringbuffer <br/> Public static void F (stringbuffer SB) {<br/> Sb. append ("ping"); <br/>}< br/>}; <br/> Class AA <br/>{< br/> Public int N; <br/>}; </P> <p>
/*************************
* Conclusion
*************************/
// Java function parameters are passed by values.
// If the parameter is of the basic type, the function cannot change the parameter value. (Including string)
// ----- Pass the passed value and pass
// If the parameter is an object, the function can change the parameter attributes (including stringbuffer and INT ).
// If the parameter is an object, the function cannot point the parameter to a new object.
// This is a good explanation. for basic types such as int, a copy of the "memory unit" that stores the int value is passed in, so the int in the function SWAp and the int outside are not a thing at all. Of course, it cannot be reflected out to influence the outside.
//
// Int. For object types, we can also think that what is passed in is a copy of the "memory unit" for storing object type pointers (although there is no pointer concept in Java, but this does not prevent us from understanding ). In this way,
//
// In the swap function, doing any operation on the value of the pointer itself will certainly not affect the integer outside, because the value in the "memory unit" of interger1 and interger2 will not change, the object type it points to has not changed.
//
// Here we need to explain a problem, that is, the stringbuffer type object. Because its content can be changed, the "Pointer" in the change function changes the stringbuffer object through operations similar "*".
//
// Itself, it is obvious. (The stringbuffer object has only one copy)
//
// Then, let's talk about C ++. The passing of basic types such as int values in it is easy, so we will not talk about it here. Another value transfer can be called pointer reference transfer.
,《Core Java 2: Volume I-fundamentalsThere is a saying like this:
The Java programming language always uses call by value. that means, the method gets a copy of all parameter values. in particle, the method cannot modify the contents of any parameter variables that are passed to it.
Extends programming versions (in particle, C ++ and Pascal) have two methods for parameter passing: Call by value and call by reference. some programmers (and unfortunately even some book authors) claim that the Java programming language uses call by reference for objects. however, that is false. because this is such a common misunderstanding, it is worth examining a counterexample in detail.
Let's try to write a method that swaps two employee objects:
public static void swap(Employee x, Employee y) // doesn't work { Employee temp = x; x = y; y = temp; }
If the Java programming language used call by reference for objects, this method wocould work:
Employee a = new Employee("Alice", . . .); Employee b = new Employee("Bob", . . .); swap(a, b); // does a now refer to Bob, b to Alice?
However, the method does not actually change the object references that are stored in the variablesAAndB.XAndYParameters ofSwapMethod are initialized with copies of these references. The method then proceeds to swap these copies.
But ultimately, this is a wasted effort. When the method ends, the parameter variablesXAndYAre abandoned. The original variablesAAndBStill refer to the same objects as they did before the method call.
// x refers to Alice, y to Bob Employee temp = x; x = y; y = temp; // now x refers to Bob, y to Alice
Program running result:
Before calling a function (the parameter is an object): 10
After calling a function (the parameter is an object): 20
Before calling a function (the parameter is of the basic type INT): 10
After calling a function (the parameter is of the basic type INT): 10
Before calling a function (the parameter is an integer of the basic type): 10
After calling a function (the parameter is of the basic type Integer): 10
Before calling a function (the parameter is string): ou
After the function is called (the parameter is string): ou
Before calling a function (the parameter is stringbuffer): Ouyang
After calling the function (the parameter is stringbuffer): ouyangping
Before calling a function (the parameter is string []):
After the function is called (the parameter is string []):
Before calling a function (the parameter is int []): 10
After calling a function (the parameter is int []): 20
To sum up
There are two ways to explain and the essence is the same.
1. From the pointer perspective, no matter what you pass, it is a pointer.
When passed, another address will be used in the function, but it points to the same content.
If you have modified the address and it has nothing to do with the address before it was passed in.
So there will be no changes. However, if you modify the pointer content, your modifications will take effect.
2. From the perspective of reference, What you pass is the reference itself, not the reference object.
If you only modify the reference itself, the object value will not be affected.
Once you modify the object value through reference, this modification will affect the object.