Problems with the Java parameter transfer method

Source: Internet
Author: User

It has always been a vexed question that the way in which Java's pass-through is passed by value or by reference, has not formed a unified opinion.

Here, I am just talking about personal opinion, not guarantee is right, all is a point.

First of all, my point is that Java uses value passing, even if it is a reference that simply passes the value of the reference to another reference, or the value is passed.

1, first for Java basic types (such as int,double,byte) and immutable type such as (String) to do the parameter must be a value to pass, such as the following program:


public class Config {public static void main (string[] args) {int a1=9; String s1= "ABC"; System.out.println ("Before Change, a1=" +a1+ ", s1=" +s1); Change (A1); Change (S1); System.out.println ("After the change, a1=" +a1+ ", s1=" +s1);} public static void Change (int a2) {a2=12;} public static void Change (String s2) {s2= "Def";}}

Because the argument is a value pass, the compiler simply passes the value of A1, S1 to A2, S2, and the value of A2 and S2 does not affect the value of A1 and S1, so it does not change.

2, for the Java object reference to do, is still the value of the pass

Only the value passed at this point is passing a reference value to another reference, as seen in the following:


public class Config {public static void main (string[] args) {StringBuffer s1=new stringbuffer ("abc"); System.out.println ("Before Change, s1=" +s1); Change (S1); System.out.println ("After the change, s1=" +s1);} public static void Change (StringBuffer s2) {s2.append ("Def");}}

At this time people may be very puzzled, why here the value of S1 has been changed.

First we understand a concept that S1 is a reference rather than an object. New StringBuffer ("abc") is an object that is stored in an in-memory heap. S1 is a reference to new StringBuffer ("abc") , equivalent to a pointer in C, where the address of the new StringBuffer ("abc") is stored, and S1 is placed inside the in-memory stack. S1 points to new StringBuffer ("abc"), such as the following:


When S1 as an actual participant, it is a reference to copy the value of S1 to S2,s2, which is equivalent to copying the address stored in the S1 to S2, at which point S2 also points to new StringBuffer ("abc"), such as the following:

Because S2 also stores the address of StringBuffer ("abc") , S2.append ("Def") is equivalent to inserting "def" directly behind the StringBuffer ("ABC "), As a result, the output S1 results have changed.

Let's look at the following example:


public class Config {public static void main (string[] args) {StringBuffer s1=new stringbuffer ("abc"); System.out.println ("Before Change, s1=" +s1); Change (S1); System.out.println ("After the change, s1=" +s1);} public static void Change (StringBuffer s2) {s2=new StringBuffer ("Def");}}

Perhaps you see this result will be more puzzled, this time S1 why did not change it??

First, the value of the reference S1 is passed to the reference S2, where S2 holds the address of the StringBuffer ("abc") , and S2=new StringBuffer ("def") This step stringbuffer ("Def the address of the ") is assigned to S2, that is, the address in S2 has changed, no longer points to StringBuffer (" abc "), that is, S1, S2 point to different objects, so the operation after S2 no longer affects S1. For example, the following:

The above is purely personal opinion, welcome all kinds of Spray!!


Problems with the Java parameter transfer method

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.