Value passing and reference passing in Java

Source: Internet
Author: User

A few days ago the interview was asked this question, and in the project also encountered similar problems, because did not summarize, resulting in a mess of answers. In the online search information, finally suddenly a lot of situation.

When an object is passed as a parameter to a method, this method can change the properties of the object and return the changed result, is it a value pass or a reference pass?

A: The value is passed. The Java programming language has only value-passing parameters. When an object instance is passed as a parameter to a method, the value of the parameter is a copy of the object's reference. Point to the same object, the contents of the object can be changed in the called method, but the object's reference (not the referenced copy) is never changed.

Java parameters, whether primitive or reference, are passed as replicas (another argument is a pass value, but a copy is better understood, and the pass value is usually relative to the address).

If the parameter type is the original type, then a copy of the parameter is passed in, that is, the value of the original parameter, which is the same as the value that was previously discussed.  If you change the value of a copy in a function, the original value is not changed. If the parameter type is a reference type, then a copy of the reference parameter is passed in, and the copy holds the address of the parameter. If you do not change the address of the copy in the function, but instead change the value in the address, the changes within the function will affect the parameters passed in. If you change the address of a copy in a function, such as new, the copy points to a new address, and the passed in parameter points to the original address, so the value of the parameter is not changed. 1: What is passed by valueRefers to when a method call passes a parameter that is passed by a copy of the value. Examples are as follows:
1  Public classTemptest {2 Private voidTest1 (inta) {3 //do something.4 }5  Public Static voidMain (string[] args) {6Temptest T =Newtemptest ();7 intA = 3;8T.test1 (a);//The parameter a passed here is passed by value9 }Ten}

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

1  Public classTemptest {2 Private voidTest1 (inta) {3A = 5;4System.out.println ("A= in Test1 method" +a);5 }6  Public Static voidMain (string[] args) {7Temptest T =Newtemptest ();8 intA = 3;9T.test1 (a);//after passing, the Test1 method changes the value of the variable without affecting the ATenSystem.out.println ("a= in the Main method" +a); One } A}

The operating result is:

The a=3 in the A=52 Main method in the 1 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:

1  Public classTemptest {2 Private voidTest1 (A a) {3 }4  Public Static voidMain (string[] args) {5Temptest T =Newtemptest ();6A A =NewA ();7T.test1 (a);//The parameter a passed here is passed by reference.8 }9 }Ten classa{ One  Public intAge = 0; A}

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:

1   Public classTemptest {2  Private voidTest1 (A a) {3A.age = 20;4System.out.println ("age= in Test1 method" +a.age);5  }6   Public Static voidMain (string[] args) {7Temptest T =Newtemptest ();8A A =NewA ();9A.age = 10;Ten T.test1 (a); OneSystem.out.println ("age= in the Main method" +a.age); A  } -  } -  classa{ the   Public intAge = 0; -}

The results of the operation are as follows:

The age=20 in the AGE=202 Main method in the 1 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:

1   Public classTemptest {2  Private voidTest1 (A a) {3A =NewA ();//Add a new line4A.age = 20;5System.out.println ("age= in Test1 method" +a.age);6  }7   Public Static voidMain (string[] args) {8Temptest T =Newtemptest ();9A A =NewA ();TenA.age = 10; One T.test1 (a); ASystem.out.println ("age= in the Main method" +a.age); -  } - } the classa{ -   Public intAge = 0; -}

The result of the operation is:

The age=10 in the AGE=202 Main method in the 1 test1 method

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

Reference Blog: http://blog.csdn.net/zzp_403184692/article/details/8184751;http://www.cnblogs.com/clara/archive/2011/09/ 17/2179493.html

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.