Java Parameter passing analysis

Source: Internet
Author: User

Original: http://www.javaranger.com/archives/1288

Let's look at the basic type as an example of parameter passing:

 Public Static void Main (string[] args) {    int param =100;    System.out.println ("Before change param=" + param);    Changeint (param);    System.out.println ("After change param=" + param);}  Public Static void changeint (int  i) {     = 1;}

When the base type is passed as a parameter, it is a copy of the passed value, no matter how you change the copy, the original value is not changed, and the result of the output proves it:
Before change param = 100
After change param = 100

Let's take a look at an example of an object passing as a parameter:

 Public Static void Main (string[] args) {        new stringbuffer ("Hello");        System.out.println ("Before change param=" +SB);        Changeobj (SB);        System.out.println ("After change param=" +SB);    }      Public Static void changeobj (StringBuffer sb) {        sb.append ("world!" );    }

First look at the output:
Before change param = Hello
After change param = Hello world!
From the results, the value of SB is changed, then it is not possible to say: When the object is passed as a parameter, it is to pass the object's reference to the past, if the reference is changed within the method, then the original object is changed. Judging from the output of the above example, this explanation is reasonable.
Now let's change the above example a little bit:

 Public Static void changeobj (StringBuffer strbuf) {    new stringbuffer ("Hi");    Strbuf.append ("world!" );}

According to the experience of the above example: When the object is passed as a parameter, the object's reference is passed through, and if the reference is changed within the method, the original object is changed as well. You would think that the output should be:
Before change param = Hello
After change param = Hi world!
But run the program and you'll see the result:
Before change param = Hello
After change param = Hello
This is a confusing place, when an object is passed as a parameter, it also changes the value of the object within the method, why some change the value of the original object, and some do not change the value of the original object? Is it a "pass-through" or "a reference"?

Look at the first program.
StringBuffer sb = new StringBuffer ("Hello");
After this sentence is executed, a SB object is generated in the heap of memory, see Figure 1:

1, SB is a reference, which is stored in an address "@3a" (This "@3a" is an example of the memory address that I lift, you just need to know the memory address is OK), and this address is "Hello" this string in memory address.
Changeobj (SB);
After executing this sentence, the SB is passed to the StringBuffer strbuf in the Changeobj method, because the address is stored in SB, the same address will be stored in strbuf, see Figure 2:

At this point, the memory addresses in SB and Strbuf are the same, so they point to "Hello".
Strbuf.append ("world!");
After executing this sentence in the Changeobj method, the value in memory pointed to by STRBUF is changed, as shown in 3:

So, Test2 this program will eventually output:
After change param = Hello world!

Then look at the second program.
Before the STRBUF = new StringBuffer ("HI") of the Changeobj method is executed, the object in memory is the same as in the previous example "Figure 2", and the STRBUF = new StringBuffer ("HI") is executed; Then it becomes:

At this point, the strbuf is no longer the address to "Hello", but the "Hi" Address "@3b" (The Same "@3b" is an example), the new operator after the successful operation will always in memory a new storage area.
Strbuf.append ("world!");
And after executing this sentence,

As you can see, because the store address in SB and STRBUF is not the same, although the value of the in-memory that strbuf points to has changed, the in-memory value that the SB points to does not change, so the following result is output:
After change param = Hello

As you can see from above, Java is a copy of the value in the stack as a parameter passed into the method. For the base type is the value of the pass, and for the object type is the reference. Whether it's a value or a reference, it's a copy of the stack. Remember this, there's no more questions.

Java Parameter passing analysis

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.