Java: Pass-by-value and Pass-by-reference detailed explanations

Source: Internet
Author: User

A detailed description of value passing and reference passing:

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:

[java]View PlainCopy
    1. Public class Temptest {
    2. Private void test1 (int A) {
    3. Do Something.
    4. }
    5. Public static void main (string[] Args) {
    6. Temptest t = new Temptest ();
    7. int a = 3;
    8. T.test1 (a); //the parameter A passed here is passed by value
    9. }
    10. }

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

Examples are as Follows:

[java]View PlainCopy
  1. Public class Temptest {
  2. Private void test1 (int A) {
  3. A = 5;
  4. System.out.println ("a= in the Test1 method" +a);
  5. }
  6. Public static void main (string[] Args) {
  7. Temptest t = new Temptest ();
  8. int a = 3;
  9. T.test1 (a); ///pass, The Test1 method changes the value of the variable without affecting the A
  10. System.out.println ("a= in the main method" +a);
  11. }
  12. }

The operating result is:

[java]View PlainCopy
    1. A=5 in The Test1 method
    2. A=3 in The Main 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:

[java]View PlainCopy
  1. Public class Temptest {
  2. Private void Test1 (a A) {
  3. }
  4. Public static void main (string[] Args) {
  5. Temptest t = new Temptest ();
  6. A = new a ();
  7. T.test1 (a); //the parameter A passed here is passed by reference
  8. }
  9. }
  10. Class a{
  11. public int age = 0;
  12. }

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:

[java]View PlainCopy
  1. Public class Temptest {
  2. Private void Test1 (a A) {
  3. A.age = 20;
  4. System.out.println ("age= in the Test1 method" +a.age);
  5. }
  6. Public static void main (string[] Args) {
  7. Temptest t = new Temptest ();
  8. A = new a ();
  9. A.age = 10;
  10. T.test1 (a);
  11. System.out.println ("age= in the main method" +a.age);
  12. }
  13. }
  14. Class a{
  15. public int age = 0;
  16. }

The results of the operation are as Follows:

[java]View PlainCopy
    1. Age= in the Test1 method
    2. Age= in the Main 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:

[java]View PlainCopy
  1. public class Temptest {
  2. private void Test1 (a a) {
  3. A = new a (); Add a new line
  4. A.age = 20;
  5. System.out.println ("age= in the Test1 method" +a.age);
  6. }
  7. public static void main (string[] Args) {
  8. Temptest t = new Temptest ();
  9. A = new a ();
  10. A.age = 10;
  11. T.test1 (a);
  12. System.out.println ("age= in the main method" +a.age);
  13. }
  14. }
  15. Class a{
  16. public int age = 0;
  17. }

The result of the Operation Is:

[java]View PlainCopy
    1. Age= in the Test1 method
    2. Age= in the Main 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:

1. An object is a reference: passing by value means that when a parameter is passed to a function, the function receives a copy of the original value

2. The original type is the pass value: passing by reference means that when a parameter is passed to a function, the function receives the memory address of the original value, not the copy of the value

3.string,integer, double, etc. immutable types because they do not provide their own modified functions, each operation is a new object, so special Treatment. Can be considered as a pass Value.

Integer is the same as String. The class variable that holds the value is the final attribute, cannot be modified, and can only be re-assigned/generated new Object. When an integer is passed into the method as a method parameter, its assignment causes the reference of the original integer to be pointed to the stack address within the method and loses its point to the original class variable Address. Any action done on the assigned integer object does not affect the original Object.

Reference: http://blog.csdn.net/zzp_403184692/article/details/8184751 (previous)

http://blog.csdn.net/fbysss/article/details/3082949 (later)

Thank you for the classic summary of our original!

(rpm) Java: Pass-by-value and pass-by-reference detailed explanations

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.