Java: Passing by value and by reference

Source: Internet
Author: User

Original Link: Portal

The day before yesterday when the system was in the Java parameter Transfer problem card, looking back to the relevant information, the parameter transfer problem has a new understanding and mastery, but there is a problem is still very vague, is Java in the end whether there is only value transmission, because in the data access, Often see some people say Java only value is passed, but some people say that the value of the pass, but also a reference to pass, for two points of view of the individual should be the point of view is different from two different statements, in fact, two of the theory of the principle is the same, as long as we understand the principle, then as to what is not matter Here is a post I saw on the internet, explaining the feeling is quite comprehensive, turn around, for later study reference:

1: What is passed by value

Refers to when a method call passes a parameter that is passed by a copy of the value. Examples are as follows:

public class Temptest {    private void test1 (int a) {        //do something    } public    static void Main (string[] args) {        Temptest t = new temptest ();        int a = 3;        T.test1 (a);//The parameter a passed here is passed by value    }}

Passing important characteristics by value: Passing a copy of a value, that is, passing it off is unrelated.

Examples are as follows:

public class Temptest {    private void test1 (int a) {        a = 5;        System.out.println ("a= in the Test1 method" + a);    }    public static void Main (string[] args) {        temptest t = new temptest ();        int a = 3;        T.test1 (a);//After passing, the Test1 method changes the value of the variable without affecting the A        System.out.println here ("a =" +a in the Main method);}    

The operating result is:

The a=3 in the A=5 Main method in the Test1 method    

  

2: What is passed by reference

When a method call is made, the passed argument is passed by reference, in fact the address of the reference that is passed, that is, the address of the memory space that the variable corresponds to.

Examples are as follows:

public class Temptest {    private void test1 (a a) {    } public    static void Main (string[] args) {        Temptest t = New Temptest ();        A = new A ();        T.test1 (a); The parameter a passed here is passed by reference    }}class a {public    int: age = 0;}

  

3: Important features passed by reference

A reference to a value is passed, meaning that both before and after delivery point to the same reference (that is, the same memory space).

Examples are as follows:

public class Temptest {    private void test1 (a a) {        a.age =;        System.out.println ("age= in the Test1 method" + a.age);    public static void Main (string[] args) {        temptest t = new temptest ();        A = new A ();        A.age = ten;        T.test1 (a);        System.out.println ("Age in the Main method =" +a.age);}    } Class A {public    int: age = 0;}

The results of the operation are as follows:

The age=20 in the Age=20 Main method in the Test1 method    

  

4: Understanding the process of passing by reference-memory allocation

To understand correctly the process of passing by reference, you must learn to understand the memory allocation process, and memory allocation can help us to understand the process.

Use the example above to analyze:

(1): Run start, run line 8th, create an instance of a, memory allocation schematic as follows:

(2): Run line 9th, is to modify the value of age in a instance, after running memory allocation is as follows:

(3): Running line 10th is the memory space address referenced by the variable A in the main method, passed by reference to the A variable in the Test1 method. Please note: These two a variables are completely different and should not be blinded by the same name.

Memory allocations are as follows:

Because it is passed by reference, that is, the address of the memory space is passed, so the new memory formed after delivery is as follows:

That is, two variables all point to the same space.

(4): Run line 3rd, assign a value to the age of the A instance that the variable a in the Test1 method points to, and the new memory that is formed after completion is as follows:

The change in the age value of the A instance is caused by the Test1 method.

(5): Run line 4th, according to the memory at this time, the output test1 method of age=20

(6): Run line 11th, according to the memory at this time, output age=20 in the Main method

5: Changes to the above example

Understanding the above example, one might ask, can you let the values passed by reference not affect each other? Is the change in the Test1 method does not affect the main method inside it?

A new instance of the Test1 method is available. Change into the following example, where the 3rd Act is added:

public class Temptest {    private void test1 (a a) {        a = new A ();//new row        a.age =;        System.out.println ("age= in the Test1 method" + a.age);    public static void Main (string[] args) {        temptest t = new temptest ();        A = new A ();        A.age = ten;        T.test1 (a);        System.out.println ("Age in the Main method =" +a.age);}    } Class A {public    int: age = 0;}

The result of the operation is:

The age=10 in the Age=20 Main method in the Test1 method    

Why is the result of this operation not the same as the previous example, or the use of memory to understand

6: Understand again by reference delivery

(1): Run start, run line 9th, create an instance of a, memory allocation schematic as follows:

(2): Run line 10th, is to modify the value of age in a instance, after running memory allocation is as follows:

(3): Running line 11th is the memory space address referenced by the variable A in the main method, passed by reference to the A variable in the Test1 method. Please note: These two a variables are completely different and should not be blinded by the same name.

Memory allocations are as follows:

Because it is passed by reference, that is, the address of the memory space is passed, so the new memory formed after delivery is as follows:


That is, two variables all point to the same space.

(4): Run line 3rd, for the Test1 method in the variable a re-generated a new instance of a, the completion of the formation of the new memory as follows:

(5): Run line 4th, assign a value to the age of the new A instance that the variable a in the Test1 method points to, and the new memory that is formed after completion is as follows:

Note: The age of variable A in the Test1 method is changed at this time, and the main method is unchanged.

(6): Run line 5th, according to the memory at this time, the output test1 method of age=20

(7): Run line 12th, according to the memory at this time, output age=10 in the Main method

7: Description

"Parameter Passing in Java is passed by value" means that passing by value is a copy of the value passed, and passing by reference is actually a reference to the address value, so collectively, by value.

  

Java: Passing by value and by reference

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.