Java parameter pass value, pass reference

Source: Internet
Author: User
Tags stringbuffer

1. Conclusion: The object type passes the reference: the underlying data type passes the value, including int, long, float, double ... and their wrapper class integer, Long, Float, Double, String ...

2, transfer value and the difference between the reference: Pass value: means that when a variable is passed to a function argument, the function's argument receives a copy of the original value, and any changes to the parameter in the function do not affect the original value; Reference: means that when a variable is passed to a function parameter, The parameter of the function receives the memory address of the original value instead of the copy of the value (the parameter and the original variable point to the same memory object), so any modification to the parameter will affect the original variable;

Instance 1: (Pass value)

void Method1 () {

int x=0;

This.change (x);

SYSTEM.OUT.PRINTLN (x);

}

void change (int i) {

I=1;

}

The output still reads: 0

Instance 2: (Passing Reference)

void Method1 () {

StringBuffer x=newstringbuffer ("Hello");

This.change (x);

SYSTEM.OUT.PRINTLN (x);

}

Voidchange (StringBuffer i) {

I.append ("world!");

}

The output is: Hello world!

Example 3: ()

1 void method1 () {

2 Stringbufferx = new StringBuffer ("Hello");

3 Change1 (x);

4 System.out.println (x);

5}

6

7 void Method2 () {

8 Stringbufferx = new StringBuffer ("Hello");

9 Change2 (x);

Ten System.out.println (x);

11}

12

Voidchange1 (StringBuffer sb) {

Sb.append ("world!");

15}

16

Voidchange2 (StringBuffer sb) {

sb= new StringBuffer ("HI");

Sb.append ("world!");

20}

Call Method1 () and the screen prints the result: "helloworld!"

Call METHOD2 (), we think the result should be "Hiworld", because SB is passing in a reference. But the actual execution result is "Hello".

Analyze change2 from the angle of memory structure:

The program executes to line 8th, and X points to a memory space that holds "Hello".

Variable x---->[store Value "Hello"]

Line 9th calls Change2 to point SB to the memory space that X is pointing to, that is, the reference to X.

Variable x \

-->[Store Value "Hello"]

Variable SB/

So far, there's nothing wrong with this, and then there's a 18 line, and here's the change of the incoming value copy: The new method does not change what SB points to in the memory, but instead opens a new space to store the string "HI", and SB points to the space.

Variable x---->[store Value "Hello"]

Variable SB---->[another piece of space to store "HI"],x the original reference is cut off, and then append to SB has nothing to do with X.

So, there is also an irregular rule: for function calls, the ultimate effect is what completely look at the implementation of the function inside. A more standard approach is to use void as a method to return a value if the referenced content is changed, but to return a new value in the return value (such as change2 in this example does not change the reference content), preferably written as a stringbuffer change2 ( StringBuffer SB) {

SB = Newstringbuffer ("Hi");

Sb.append ("world!");

return SB;

}

)。

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.