Home > Java > javaTutorial > body text

Detailed explanation of reference and value passing in Java

巴扎黑
Release: 2017-07-20 13:11:20
Original
1412 people have browsed it

Let’s look at a piece of code first, which is a written interview question I encountered:

 1 /** 2  * ***面试题 3  */ 4 public class StringBufferTest { 5     public static void main(String[] args) { 6         int i = 10; 7         test(i); 8         System.out.println(i);//10 9         StringBuffer str = new StringBuffer("str");10         test2(str);11         System.out.println(str.toString());//str_add12         test3(str);13         System.out.println(str.toString());//str_add14     }15 16     public static int test(int m) {17         m = 30;18         return m;19     }20 21     public static StringBuffer test2(StringBuffer str2) {22         str2.append("_add");23         return str2;24     }25 26     public static StringBuffer test3(StringBuffer str3) {27         str3 = new StringBuffer("java");28         return str3;29     }30 31 }
Copy after login
View Code

Value transfer:

The value of i will not become 30 because the input parameter is assigned a value of 30 in the test method. Therefore, the method test(i ), the value of i printed is still the previous value 10

The specific process is as follows:

Variable i---->[Storage value 10]

Execute the call test( When using the i) method, the situation in the memory is as follows: i copies its own value in the memory, and then the variable m points to the copied 10.

Variable i---->[Storage value 10]

                                                            ↓ A value copy was made


Variable m--->[Storage value 10]

At this time, when execution reaches line 17, the variable m is assigned a value of 30, and the operation of this step has nothing to do with i.

Variable m--->[Storage value 30]

Pass by reference: (When using global variables, pay special attention to whether the change will affect other places)

The str passed to the test2 method is a reference to str, so changes in str2 will affect the value of str

Therefore, after executing the method test2(str), the value of str printed becomes str_add

The specific process is as follows:

Variable str---->[Storage value "str"]

Execute test2(str). Note that this is essentially different from value transfer. : When calling test2(str), the variable str2 also points to the memory space pointed to by str, rather than pointing to a copy of str.

Variable str \

                                                                                                                                                                                                                    have have The value of the memory space and the value of str will also change accordingly.


Variable str \
                                                                                                                                             str The value is not java but str_add?

The main reason is: str3 = new StringBuffer("java"); This line of code

changes similar to the copy of the incoming value: the new method does not change the content of the memory pointed by str, but A new space is opened up to store the string "java", and str3 points to this space.

The specific process is as follows:

The program executes to line 12, and str points to a memory space where "str_add" is stored.

Variable str---->[Storage value "str_add"]

Call test3(str) and point str3 to the memory space pointed by str, which is the reference of passed in str.

Variable str \

                                                                                                                                                                       .com.com to                                  " : The new method does not change the content of the memory pointed to by str, but opens up a new space in it to store the string "java", and str3 points to this space.

Variable str---->[Storage value "str_add"]

                                     The original reference of str3 is cut off

                                                                                                                                       who’ who’ who’ who who who who’ who who’ who who who’ who who who who who who who who who who who who who’s which, which is where, value “str_add” is stored ]

 

 


 

The above is the detailed content of Detailed explanation of reference and value passing in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template