Java pass-by-value and pass-by-reference narration

Source: Internet
Author: User

Read a post on the internet to explain the value of Java and pass by reference, feel 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) {tempt    EST 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);    ///pass, the Test1 method changes the value of the variable without affecting the A  System.out.println ("a= in the Main method" +a); }}

  

The operating result is:

A=3 in the A=5main 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:

//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 = 20;  System.out.println ("age= in Test1 Method" +a.age);    public static void Main (string[] args) {temptest t = new temptest ();    A = new A ();    A.age = 10;    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:

Age=20 in the Age=20main 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 13th, create an instance of a, memory allocation schematic as follows:

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

(3): Running line 15th 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 5th, 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 6th, according to the memory at this time, the output test1 method of age=20
(6): Run line 16th, 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 ();   The new row a.age = 20;  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 = 10;    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:

Age=10 in the Age=20main 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 14th, create an instance of a, memory allocation schematic as follows:

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


(3): Running line 16th 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 5th, 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 6th, 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 7th, according to the memory at this time, the output test1 method of age=20
(7): Run line 17th, according to the memory at this time, output age=10 in the Main method
7: Description
(1): "In Java parameter passing is passed by value" This sentence means: Pass by value is a copy of the value passed, by reference is actually passed the value of the referenced address, so collectively by value passed.
(2): There are only basic types in Java and strings that are defined in the following way are passed by value, others are passed by reference. is to define the string directly using double quotation marks: string str = "Java private";


Original link: 8184751

Java pass-by-value and pass-by-reference narration

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.