Value passing and reference passing in Java

Source: Internet
Author: User

Value passing and reference passing were used in recent work, but a bit of a rip-off, now read the following article after the clearer. This is the article (NET pick)

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): "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";

Value passing and reference passing in Java

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.