Press value in the Java application semantics

zhaozj2021-02-08  392

The election understanding parameter is a value transfer mechanism for the value of the value of the Java application, that is, the value of the Java application is passed by reference. Writing it is to expose a common existence of a common existence, that is, the Java application is transmitted in accordance with reference to avoid common programming errors due to the behavior of "reference" by reference ".

Some feedback on this savings think that I am confused this question, or completely mistaken it.

Many do not agree to my readers as an example of using C languages. Therefore, in this column I will use C and Java applications to further clarify some facts.

After reading all the reviews, the problem finally understood, at least in a major issue. Some comments think that my feature is wrong, because the object is passed by reference. Objects are indeed transmitted by reference; the excerpt is in conflict with this. The election says that all parameters are values ​​- another parameter - passed. The following statement is correct: I will never pass the object in the Java application, and only the object reference is passed. Therefore, the object is transmitted according to the reference. But it is important to distinguish between the parameters, this is the intention of the selection. The fact that Java applications is transmitted by reference to the object does not mean that the Java application passes parameters by reference. The parameter can be an object reference, and the Java application is referenced by value transfer object.

Parameters in C and Java applications Pass variables in the Java application can be one of the following two types: reference type or basic type. When it is passed to a method, the way to handle these two types is the same. Both types are passed by values; no one is passed by reference. This is an important feature, as shown in the subsequent code example. Before continuing to discuss, it is important to define the two terms that are passed by reference to the reference. Press value conversion means that when a parameter is passed to a function, the function receives a copy of the original value. Therefore, if the function modifies the parameter, only the copy is changed, while the original value remains unchanged. According to reference, it means that when a parameter is passed to a function, the function is received is the memory address of the original value, not a copy of the value. Therefore, if the function modifies this parameter, the original value in the call code also changes. Some confuses from the parameter pass in the Java application stem from the fact that many programmers are programmed from C to Java programming. C includes non-reference types, including reference types, and values, values, and by reference, respectively. The Java programming language has basic types and object references; therefore, it is considered that the Java application is transmitted to the basic types like C , and the reference to reference is logically logically. After all, you will think so, if you are passing a reference, it must be passed by reference. It is easy to believe this, I actually I believe this, but this is incorrect. In C and Java applications, when the parameters pass to the function are not referenced, the passed is a copy of this value (transmitted by value). The difference is to reference. When the parameters pass to the function in C are referenced, you passed this reference, or a memory address (by reference delivery). In the Java application, when the object reference is a parameter passed to the method, you passed a copy of the reference (passing the value), not the reference itself. Note that object references and copies of the calling method points to the same object. This is an important difference. The Java application is not different from C when passing different types of parameters. The Java application passes all parameters by value, which makes a copy of all parameters regardless of their type.

Example We will use the previous definition and discussion analysis some examples. First consider a C code. C language simultaneously uses value transfer and parameter delivery mechanism passed by reference: Listing 1: C sample #include

#include

Void Modify (int A, int * p, int & r);

INT main (int Argc, char ** argv)

{

Int Val, REF;

INT * PINT;

VAL = 10;

REF = 50;

Pint = (int *) malloc (sizeof (int));

* PINT = 15;

Printf ("VAL IS% D / N", VAL);

Printf ("PINT IS% D / N", PINT);

Printf ("* PINT IS% D / N", * PINT);

Printf ("REF IS% D / N / N", REF);

Printf ("Calling Modify / N");

// Press value to pass VAL and PINT, and pass the Ref as reference.

Modify (VAL, PINT, REF);

Printf ("Returned from modify / n / n");

Printf ("VAL IS% D / N", VAL);

Printf ("PINT IS% D / N", PINT);

Printf ("* PINT IS% D / N", * PINT);

Printf ("REF IS% D / N", REF);

Return 0;

}

Void Modify (int A, int * p, int & r)

{

Printf ("in modify ... / n");

A = 0;

* p = 7;

P = 0;

R = 0;

Printf ("a% d / n", a);

Printf ("P IS% D / N", P);

Printf ("R IS% D / N", R);

}

The output of this code is: Listing 2: Output of C code

VAL IS 10

Pint IS 4262128

* PINT IS 15

REF IS 50

Calling modify

In Modify ...

A is 0

P IS 0

R IS 0

Returned from modify

VAL IS 10

Pint IS 4262128

* PINT IS 7

REF IS 0 This code declares three variables: two integer variables and a pointer variable. The initial value of each variable is set and print it out. At the same time, the pointer value and the value indicated thereto are printed. Then pass all three variables to the Modify function as the parameters. The first two parameters are passed by value, and the last parameter is passed by reference. The function prototype of the Modify function indicates that the last parameter is passed as a reference. Recall that C is transmitted all parameters by value, except the reference, the latter is passed according to the reference. The Modify function changes the value of all three parameters:

Set the first parameter to 0. Set the value points to the second parameter to 7 and set the second parameter to 0. Set the third parameter to 0. Print the new value, then the function returns. When the return to main is performed, the value of these three parameters is reproduced again and the value points to the pointer. The variables transmitted as the first and second parameters are not affected by the MODIFY function, as they are passed by values. But the value indicated by the pointer changes. Note that variables transmitted as the last parameter are changed by the Modify function as the previous two parameters, because it is passed by reference. Now consider similar code written in Java language: Listing 3: Java application Class Test

{

Public static void main (string args [])

{

Int Val;

StringBuffer SB1, SB2;

VAL = 10;

SB1 = New StringBuffer ("apples");

SB2 = New StringBuffer ("pears");

System.out.println ("VAL IS" VAL);

System.out.println ("SB1 IS" SB1);

System.out.println ("SB2 IS" SB2);

System.out.println ("");

System.out.Println ("Calling Modify");

// Pass all parameters by value

Modify (VAL, SB1, SB2);

System.out.println ("Returned from Modify");

System.out.println ("");

System.out.println ("VAL IS" VAL);

System.out.println ("SB1 IS" SB1);

System.out.println ("SB2 IS" SB2);

}

Public Static Void Modify (Int A, StringBuffer R1,

StringBuffer R2)

{

System.out.println ("in modify ...");

A = 0;

R1 = NULL; // 1

R2.Append ("taste good");

System.out.println ("a is" a);

System.out.println ("R1 IS" R1);

System.out.println ("R2 IS" R2);

}

} The output of this code is: Listing 4: Output of the Java application

VAL IS 10

SB1 IS Apples

SB2 IS Pears

Calling modify

In Modify ...

A is 0

R1 is null

R2 is Pears Taste Good

Returned from modify

VAL IS 10

SB1 IS Apples

SB2 is Pears Taste Good This code declares three variables: a integer variable and two object references. The initial value of each variable is set and print it out. All three variables are then passed to the Modify method as the parameters. The Modify method changes the values ​​of all three parameters:

Set the first parameter (integer) to 0. Set the first object reference R1 to NULL. The second reference R2 is retained, but by calling the APPEND method to change the object it referenced (this is similar to the processing of pointer P in the previous C example). When performing returns to main, the values ​​of these three parameters are reproduced again. As expected, the integer VAL has not changed. Object reference SB1 has not changed. If SB1 is passed by reference, as many people claim, it will be NULL. However, since all parameters are passed by the Java programming language, one copy of the reference to the SB1 is passed to the Modify method. When the modify method sets R1 to NULL at // 1, it is only a copy of the reference to SB1, rather than operates like C . Also note that the second object reference SB2 is printed is a new string set in the Modify method. Even if the variable R2 in Modify is just a copy of SB2, they point to the same object. Therefore, the method called by the replicated reference is the same object. Writing a switching method assumes how we know how the parameter is passed, and a swap function in C can be done in a different way. Use the switching function of the pointer to the following code, where the pointer is passed by value: Listing 5: Use the switching function of the pointer

#include

#include

Void Swap (int * a, int * b);

INT main (int Argc, char ** argv)

{

INT VAL1, VAL2;

VAL1 = 10;

VAL2 = 50;

SWAP (& Val1, & Val2);

Return 0;

}

Void Swap (int * a, int * b)

{

INT TEMP = * B;

* b = * a;

* a = TEMP;

}

Use the referenced exchange function Similar to the following code, where reference is passed by reference: Listing 6: Use the referenced exchange functions

#include

#include

Void Swap (INT & A, INT & B);

INT main (int Argc, char ** argv)

{

INT VAL1, VAL2;

VAL1 = 10;

VAL2 = 50;

SWAP (VAL1, VAL2);

Return 0;

}

Void Swap (Int & A, Int & B)

{

INT TEMP = B;

B = a;

a = TEMP;

}

The two C code examples exchange values ​​as desired. If the Java application uses "by reference", the following switching method should work just as the C sample: Listing 7: whether the Java exchange function is transmitted as reference in C

Class swap

{

Public static void main (string args [])

{

Integer a, b;

a = new integer (10);

b = new integer (50);

System.out.Println ("Before Swap ...");

System.out.println ("a is" a);

System.out.println ("b IS" b);

SWAP (A, B);

System.out.println ("After Swap ..."); System.out.println ("a is" a);

System.out.println ("b IS" b);

}

Public Static Void SWAP (Integer A, Integer B)

{

Integer temp = a;

A = B;

B = TEMP;

}

} Since all parameters are passed by the Java application, this code will not work properly, and the generated input is as follows: Listing 8: Listing 7 output

BEFORE SWAP ...

A is 10

B IS 50

After swap ...

A is 10

b is 50, then how to write a method in the Java application to exchange two basic types of values ​​or two object references? Because the Java application passes all parameters by value, you can't do this. To switch the value, you must be used in the method to call the external inline.

Conclusion I am in the book. The intention of this information is not a tutorial analysis or trying to make problems complicated, but want to warn programmers: assuming that "delivery" is dangerous in the Java application "semantics. If you assume the Semantics in the Java application, you might write a similar to the above switching method, then doubt why it does not work properly. I have to admit that when I first realized that the Java application passed all the parameters, I also expressed doubts. I have been assumed that because Java applications have two types, they pass the basic type by value to pass the reference delivery reference, just like C . Before turning to Java programming, I have used C for several years, and I feel that any other things seem to be intuitive. However, once I understand what happened, I believe that the method of passing all parameters in the Java language is more intuitive. The author of THE JAVA Programming Language, Second Edition is the best in Section 2.6.1: "There is only one parameter transmission mode in Java - transferred by value - this helps to keep things simple."

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

New Post(0)