Java is a pass value or a pass reference

zhaozj2021-02-17  89

1. Simple type is passed by value

The parameter of the Java method is a simple type, which is passed by value (pass by value). At this point, we can explain with 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);

}

}

operation result:

Before test (boolean): test = TRUE

In test (boolean): test = false

After test (boolean): test = TRUE

It is not difficult to see that although the value of the passed parameters is changed in the TEST (Boolean) method, there is no impact on this parameter source variable, that is, the TEST variable in the main (String []) 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 a reference

Java is a pass value or a bi-reference, the problem is mainly on the passage of the object, 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. such as

String a = "hello";

String b = a;

Here, A and B are two 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 will take an example with StringBuffer: / * 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);

}

}

operation result:

A is Hello, World

In this example, both A and B are referenced. When the value of the object indicated by B is changed, the value of the object indicated by A is also changed from the output result. Therefore, both A and B are part of the STRINGBUFFER object that contains "Hello" to the same object.

Here I describe two points:

1. Quote is a data type that saves the address in memory in memory. This type is not what we usually say is not a class instance (object); 2. Different references may point to the same object, In other words, an object can have multiple references, that is, the variable of the type of type.

3. How is the object passed?

There are two ways to pass on objects, ie "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 (StringBuffer Str) {

Str.Append (", World!");

}

Public static void main (String [] args) {

StringBuffer string = new stringbuffer ("hello");

Test (String);

System.out.println (String);

}

}

operation result:

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 see 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);

}

}

operation result:

Hello

Why is this so? 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 correctly or transmitted?

To see this question correctly, you must figure out why there will be 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);

}

operation result:

1: a = 0, b = 1

2: a = 0, b = 1

3: a = 1, b = 0

You can clearly see that pressing parameters can be easily modified by parameters, while the value is passed.

When Java grows, many C programmers began to turn to learning Java, they found that using similar SwapValue can not change the value of the simple data type passed through the parameters, but if it is an object, it may be members Free changes. So they think this is very like a question of the value / pass pointer in the C language. But there is no pointer in Java, then this problem evolves into a problem of passing value / transmission. Unfortunately, this problem is not appropriate in Java. The ultimate goal of discussing such a problem is just to make it easy to change the value of the parameters in the method function and make it a long time.

In Java, there are two cases of changing parameters. The first, the assignment number "=" is used directly to change, as Examples 1 and Example 4; second, for some objects, through a certain way Change their member data, as Example 3. For the first case, the change does not affect the data other than the method, or directly said source data. In the second method, the opposite will affect the source data - because the object indicated by the reference indication is not changed, the object is changed, and the object is substantially changed.

5. How to implement a method similar to SWAP

The issue is still a problem, it is already solved, but we still can't solve such a problem: If I have two INT type variables A and B, I want to write a way to exchange their value, what should I? do?

Conclusion is very disappointed - there is no way! Therefore, we can only specifically discuss specific conditions to use the exchanging method to use the exchange method as an example:

/ ** Example 6 * /

/ **

* @ (#) Test.java

* @Author fancy

* /

Public class test {

Public static void swap (int [] data, int a, int b) {

INT T = DATA [A];

Data [A] = DATA [B];

DATA [B] = T;

}

Public static void main (String [] args) {

Int [] data = new int [10];

For (int i = 0; i <10; i ) {

Data [i] = (int) (math.random () * 100);

System.out.print ("" DATA [I]);

}

SYSTEM.OUT.PRINTLN ();

For (int i = 0; i <9; i ) {

For (int J = i; j <10; j ) {

IF (Data [I]> Data [J]) {

SWAP (Data, I, J);

}

}

}

For (int i = 0; i <10; i ) {

System.out.print ("" DATA [I]);

}

SYSTEM.OUT.PRINTLN ();

}

}

Operation results (one of the cases):

78 69 94 38 95 31 50 97 84 1

1 31 38 50 69 78 84 94 95 97

SWAP (int [] data, int a, int b) method actually changes the member data of the object indicated by the DATA, that is, the second change parameter value of the above discussion. I hope that everyone can give an Anti-three. Use similar methods to solve the problem.

转载请注明原文地址:https://www.9cbs.com/read-31321.html

New Post(0)