Value passing and reference passing issues in Java

Source: Internet
Author: User

It's a cliché and a lot of controversy, but there's never been a satisfactory answer to that question.

Some people have concluded:

    1. Objects are passed by reference.
    2. Java applications have only one parameter passing mechanism, which is passed by value
    3. Passing by value means that when a parameter is passed to a function, the function receives a copy of the original value
    4. 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

Simple summary:

    1. An object is a reference (a copy of a reference address, and a value in essence).
    2. The original type is the pass value
    3. Immutable types such as string 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.
A small problem

In open source China sees a problem like this:

https://www.oschina.net/question/2507499_2244027, what is the output of the variable a before and after?

I answered wrong, I think that the function is passed in the main functions of a, in function to modify the address of a, so back to the main function, A's address has become the function of the A2 assigned to the address, so after function processing a value has changed. But it didn't, because I overlooked one of the basics of Java : Theargument in Java is a value pass , and if it's the base type, it's a copy of the value , and if it's the object, it's a copy of the reference address.

Basic type of delivery

The following is the processing class porcess, and the code should already be self-explanatory. Function1 is to change the argument A to 2,function2 is to initialize int B, assign a value of 5, and then assign B to a:

 Public class Process {    publicvoid function1 (int  a) {        = 2;    }      Public void function2 (int  a) {        int b = 5;         = b;    }}

We continue to see the test class Testprimitive:

 Public class testprimitive {    publicstaticvoid  main (string[] args) {          New  Process ();         int age =;        System.out.println (age);        Process.function1 (age);        System.out.println (age);    }}

After function1 processing, the result is:

1818

After modifying the test class, after function2 processing, the result is:

1818

Conclusion: The basic type of the parameter is changed, and the value of the original argument is not affected.

Object type pass-through parameter

The following is the processing class Porcess,function3, which sets the color of the parameter car to blue. Function4, a new car2 was created, assigning the car2 to the parameter car.

 Public class Process {    publicvoid  function3 (car car) {        car.setcolor ("Blue");    }      Public void function4 (car car) {        new car ("Black");         = car2;        Car.setcolor ("Orange");}    }

We continue to see the test class Testreference

 Public class testreference {    publicstaticvoid  main (string[] args) {          New  Process ();         New Car ("Red");        SYSTEM.OUT.PRINTLN (car);        Process.function3 (car);        SYSTEM.OUT.PRINTLN (car);}    }    

The result is that after function3 processing, the output is:

Car{color= ' Red '}car{color= ' Blue '}

Modify the test class after function4 processing:

Car{color= ' Red '}car{color= ' Red '}

Conclusion: The parameters of the object type can be modified directly by invoking the parameter set method. If you modify the pointing address of a parameter, call the set method of the pass parameter, and you cannot modify the value of the original argument.

In summary, the basic type of the parameter, in the method is a copy of the value, there is a new local variable to get this value, the modification of this local variable does not affect the original parameters. The argument of the object type, passing the address on the heap, inside the method there is a new local variable to get a copy of the reference address, the operation of the local variable, the effect is the same piece of address, so the original parameters will also be affected, conversely, if the local variable to modify the reference address, the original parameter will not have any possible impact.

Value passing and reference passing issues 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.