Pass and pass a reference for the value of JAVA

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https: //blog.csdn.net/Norte_L/article/details/80250057

 

Sort of fuzzy concept before, as I understand it is passed by reference in C-like pointer.
A basic understanding and reference types of
data types in Java is divided into two basic types and reference types.

1, the basic types of variables to save the original value, the variable is the data itself.

    Common basic types: byte, short, int, long, char, float, double, Boolean, returnAddress.

2, reference type variable holds a reference value, the reference value is the so-called "first address value" where the object's memory space, by this reference value to manipulate objects.

    Common reference types: class types, interface types and arrays.

Second, the value of the transmission and passed by reference understanding
1, the value transfer

    In the method invocation, the argument passed to its actual parameter values, the value of this transfer process is a copy of the argument passed to a function, so that if (parameter values) of the value of the function operation is performed will not affect the value of the argument. Because it is directly copied, so this way when the large amount of data transfer, the operating efficiency will be particularly low.

2, passed by reference

    Pass a reference to make up for the lack of value transfer, to a large amount of data transfer, direct complex in the past, it would take up a lot of memory space, and pass by reference is the address of the object passed by value in the past, the function received is the first address of the original value value. During the execution of the method, the parameter and the parameter of the same content, a point to the same memory address, i.e. in fact the operation of the source data, the execution of the method will affect the actual object.

for example:

public class Example {
String str = new String("hello");
char[] ch = {'a', 'b'};
public static void main(String[] args) {
Example ex = new Example();
ex.change(ex.str, ex.ch);
System.out.println(ex.str + " and");
System.out.println(ex.ch);
}

public void change(String str, char[] ch) {
str = "ok";
ch[0] = 'c';
}
}
输出是:

hello and

cb

Process analysis:

1, the space allocated for the object

 

2, the implementation of change () method

Point before executing arguments (black) and parameter (red) as follows:

 

Because String class are immutable and are passed by value, the CH [] is passed by reference, the method str = "ok", corresponding to recreate an object does not change the value of the argument str, array passed by reference, direct change, so after performing the method, the following relationship point:

 

3. Conclusion
Through the above analysis we can draw the following conclusions:

The basic data types traditional values, parameter modification does not affect the argument;
reference-reference type, and parameter arguments refer to the same memory location (the same object), the influence of the parameters to be modified on the actual object.
String, Integer, Double immutable types of special handling, etc., as will be appreciated by value, the last operation does not modify the argument object.

Guess you like

Origin www.cnblogs.com/dblb/p/11431447.html