Parse Java to pass by value or by reference, parse java to pass reference

Source: Internet
Author: User

Parse Java to pass by value or by reference, parse java to pass reference

1: What is passed by value?

It indicates that when a method is called, The transmitted parameters are copied and transmitted by value. Example:

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 }}

An important feature of passing by value: It transfers the copy of the value, that is, the transfer is irrelevant.

Example:

Public class TempTest {private void test1 (int a) {a = 5; System. out. println ("a =" + a);} public static void main (String [] args) {TempTest t = new TempTest (); int a = 3; t. test1 (a); // after passing, the change of the variable value of test1 does not affect the System. out. println ("a =" + a) in the main method );}}

The running result is:

In the test1 method, a = 5. In the main method, a = 3.

2: What is passed by reference?

It indicates that when a method is called, The passed parameters are passed by reference. In fact, the passed reference address is the address of the memory space corresponding to the variable.

Example:

Public class TempTest {private void test1 (A a) {} public static void main (String [] args) {TempTest t = new TempTest (); A a = new (); t. test1 (a); // The parameter a passed here is passed by reference} class A {public int age = 0 ;}

3: Important features of transferring by reference

The value reference is passed, that is, both before and after the transfer point to the same reference (that is, the same memory space ).

Example:

Public class TempTest {private void test1 (A a) {. age = 20; System. out. println ("age =" +. age);} public static void main (String [] args) {TempTest t = new TempTest (); A a = new A ();. age = 10; t. test1 (a); System. out. println ("age =" +. age) ;}} class A {public int age = 0 ;}

The running result is as follows:

Age = 20 in the main method of test1 = 20

4: understand the process of transferring by reference-memory allocation

To correctly understand the process of transferring by reference, you must understand the process of memory allocation. memory allocation can help us understand this process.

The following example is used for analysis:

(1): The instance starts to run, runs row 8th, and creates an instance named A. The memory allocation diagram is as follows:


(2): Run row 9th to modify the age value of instance A. The memory allocation after running is shown as follows:


(3): Run row 10th to pass the memory space address referenced by variable a in the main method to the variable in the test1 method according to the reference. Note: These two a variables are completely different and should not be blinded by the same name.

The memory allocation diagram is as follows:


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


That is to say, both variables point to the same space.

(4) Run row 3rd and assign a value to the age of instance A pointed to by variable a in test1. The new memory is formed as follows:


In this case, the age value of instance A is changed by the test1 method.

(5): run the 4th rows. Based on the memory at this time, the age = 20 in the test1 method is output.

(6): run the 11th rows. Based on the memory at this time, the age = 20 in the main method is output.

5. Changes to the above example

After understanding the above example, someone may ask, can we keep the value passed by reference without affecting each other? Is the modification in the test1 method not affecting the main method?

The method is to create a new instance in the test1 method. Change to the following example, of which 3rd behavior is newly added:

Public class TempTest {private void test1 (A a) {a = new A (); //. age = 20; System. out. println ("age =" +. age);} public static void main (String [] args) {TempTest t = new TempTest (); A a = new A ();. age = 10; t. test1 (a); System. out. println ("age =" +. age) ;}} class A {public int age = 0 ;}

The running result is:

Age = 20 in the main method of test1 = 10

Why is the running result different from the previous example? Use the memory to understand it.

6: Understand transfer by reference again

(1): The instance starts to run, runs row 9th, and creates an instance named A. The memory allocation diagram is as follows:

(2): Run row 10th to modify the age value of instance A. The memory allocation after running is shown as follows:

(3): Run row 11th to pass the memory space address referenced by variable a in the main method to the variable in the test1 method according to the reference. Note: These two a variables are completely different and should not be blinded by the same name.

The memory allocation diagram is as follows:

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

That is to say, both variables point to the same space.

(4): Run row 3rd to generate a new A instance for variable a in the test1 method. The new memory is as follows:

(5) Run row 4th and assign a value to the age of the new A instance pointed to by variable a in the test1 method. The new memory is formed as follows:

Note: The age of variable a in the test1 method is changed at this time, while that in the main method is not changed.

(6): Run 5th rows. Based on the memory at this time, output age = 20 in the test1 method.

(7): run the 12th rows. Based on the memory at this time, the age = 10 in the main method is output.

7: Description

(1): "parameter passing in Java is passed by value" means that passing by value is a copy of the passed value, passing by reference actually transfers the referenced address value, so it is collectively referred to as passing by value.

(2): in Java, only the basic type and String defined in the following way are passed by value, while others are passed by reference. Double quotation marks are directly used to define the String: String str = "Java ";

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.