Java is a pass value or a bi-reference. This estimate is very confused now. Here is the article written by it. You can look at it. . This is still more clear, just in depth. 1. Simple Type is the parameter of the Java method passed by the value is a simple type, and it is passed by value (Pass By Value). This point we can explain to a simple example: / * Example 1 * // *** @ (#) Test.java * @Author fancy * / public class test {public static void test (boolean test) {test = ! Test; System.Out.println ("in test (boolean): test =" test);} public static void main (string [] args) {boolean test = true; system.out.println ("Before test) Boolean: test = " test); test (TEST); system.out.println (" After test (boolean): test = " test);}} Run Results: Before test (boolean): test = truein Test (boolean): Test = falseafter test (Test = True is not difficult to see, although the value of the passing parameters is changed in the TEST (Boolean) method, but there is no impact on this parameter source variable itself, that is, main (String []) The TEST variable in the method has no effect. The description, when the parameter type is simple type, it is passed by value. When passing a simple type variable in parameter form, it is actually a copy of the value of the parameters. So how to change its value in the method function, the result is only changed the value of the copy, and Not a source value. 2. What is the reference to java is a pass value or a bi-pass, the problem is mainly on the pass, because Java has no reference. Since the debate is referred to this thing, in order to figure out this problem, we must know what the reference is. Simply put, the reference is actually like an object's name or alias (alias), an object requests a space in memory to save data, depending on the size of the object, it may need to take up the space size. When accessing the object, we will not directly access the objects in memory, but to access it by reference. Quote is also a data type, we can imagine what is similar to the pointer in the C language, which indicates the address in the memory - just what we can't observe this address is. If we define more than one reference point to the same object, then these references are different because references are also a data type, requiring a certain memory space to save. However, their values are the same, indicating the same object in memory. For example, string a = "hello"; string b = a; Here, A and B are different references, we use two definition statements to define them. But their value is the same, all point to the same object "Hello".
Maybe you still feel that it is not intuitive, because the value of the String object is not changeable (like b = "world"; b = a; this situation is not changed "world" object, but changed it The value of the reference B points to another String object a). Then we use StringBuffer to give an example: / * Example 2 * // *** @ (#) Test.java * @Author fancy * / public class test {public static void main (string [] args) {StringBuffer a = New StringBuffer ("Hello"); StringBuffer B = A; B.Append (", World"); System.out.Println ("a is" a);}} Run Result: a is hello, World This example Both A and B are referenced, when changing the value of the object indicated, from the output result, the value of the object indicated also changes. Therefore, both A and B are part of the STRINGBUFFER object that contains "Hello" to the same object. Here I describe two points: 1. Reference is a data type, saved the address of the object in memory, this type is not what we usually said is not a class instance (object); 2. Different The reference may point to the same object. In other words, an object can have multiple references, which is the variable of the type of type. 3. How does the object pass? There are two statements about the pass, that is, "it is transmitted by value" and "it is passed by reference". These two statements have their own truth, but they have not analyzed from essentially, which is caused to argue. Since we already know what is referenced, then now to analyze how the object is how the parameters are passed. Still first take a program as an example: / * Example 3 * // *** @ (#) Test.java * @Author fancy * / public class test {public static void test (Str.Append ", World! ");} Public static void main (string [] args) {stringbuffer string = new stringbuffer (" hello "); test (string); system.out.println (String);}} Run: Hello, World Test (string) calls the TEST (STRINGBUFFER) method and passes the string as a parameter. Here String is a reference, this is not doubtful. As mentioned earlier, the reference is a data type, and it is not an object, so it cannot be passed by reference, so it is passed by value, it is what it is? Is the address of the object.
This shows that the object is passed when the object is passed, right? wrong! Why is wrong, let's look at another example: / * Example 4 * // *** @ (#) Test.java * @Author fancy * / public class test {public static void test (string str) {str = "World "} public static void main (string" args) {string string = "hello"; test (string); system.out.println (String);}} Run results: Hello why? Because the parameter STR is a reference, it is different from String, although they are references to the same object. Str = "world" changed the value of the STR, which points to another object, but the object points to the Str change, but it does not have any effect on "Hello", and because String and Str are different references, The change of the STR has no effect on String, as shown in example. The result is that the parameter is overthrown. So, when the object is passed as a parameter? Also wrong! Because the previous example is indeed, it is passed by value. As a result, just like light is like a wave or a particle, the parameter of the Java method is what is transmitted. The answer can only be: 即 是 是 传 传 传 传 是 传 是 是 是 是 是 是 是 是 是different. 4. What is the problem that correctly looks forward to the value or a reference to the correct look, this problem must be found, why do you have such a problem. In fact, the problem is derived from C instead of Java. In the C language, there is a pointer, so when a data is passed to a function as a parameter, there are two ways: passing values, or a pointer, their difference, can be used as a simple example: / * Example 5 * // *** @ (#) Test.c * @Author fancy * / void swapvalue (int A, int b) {INT T = a; a = b; b = t;} void swappointer INT * a, INT * B) {INT T = * a; * a = * b; * b = T;} void main () {Int a = 0, b = 1; Printf ("1: a =% D , B =% D / N ", A, B); SwapValue (A, B); Printf (" 2: A =% D, B =% D / N ", A, B); Swappointer (& A, & B) PRINTF ("3: A =% D, B =% D / N", A, B);} Run Result: 1: A = 0, B = 12: a = 0, b = 13: a = 1, b = 0 You can see it clearly that the parameters can be easily modified by the pointer to pass the value passed through the parameters, while the payment is not passed.