Values passed by the Java parameter Pass

Source: Internet
Author: User

an overview1. What is parameter passing?

the process of passing data to a parameter when calling a method is called argument passing. There are two ways of passing in a programming language: value passing and reference passing. It must be emphasized that the two delivery methods mentioned here are not limited to the way in which Java is used, but are delivered in a variety of programming languages, including Java.

2. Variable type

In Java, we refer to the variable that points to the primitive type data as the original variable, and the variable that points to the object is called the reference variable.

Two value pass1. What is value passing?

A copy of the variable is passed into the method, the method is isolated inside and outside, and the operation of the variable within the method is not reflected in the variable outside the method.

2. Primitive Variables
    publicvoid change (int  b) {        = 7;    }    @Test    publicvoid  testbasic () {        int a = 9;        Change (a);        System.out.println (a);    }

Actual output: 9

When a parameter is passed, the rule passed by value, variable B receives a copy of a and also points to the literal "9":

Next, within the method, assign a value of 7 to B, so that B points to 7, since A and B are two independent variables, there is no reference to the referenced relationship, and a still points to 9:

3.String
     Public void Change (String str01) {        = "Baidu";    }    @Test    publicvoid  teststring () {        new String ("www.baidu.com ");        Change (str);        System.out.println (str);    }

Actual output: www.baidu.com

When the parameter is passed, str passes a copy of itself to Str01, so that Str01 also points to the object in the heap that holds "www.baidu.com":

Assign a value to Str01 inside the method so that Str01 points to "Baidu" in the method area string constant pool, and Str still points to "www.baidu.com" in the heap, where str and Str01 point to different objects without affecting each other:

One thing to note here is that Java designs a string as an immutable object, that is, once the string object contains a literal change, Java creates a new object and points the variable to the new variable.

4.StringBuilder
     Public void Change (StringBuilder builder01) {        builder01.append ("world!") );    }    @Test    publicvoid  Teststringbuilder () {        new StringBuilder ("Hello");        Change (builder);        System.out.println (builder);    }

Actual output: Hello world!

After the argument is passed, the BUILDER01 variable obtains a copy of the builder variable that points to the same object in the heap as the original variable:

Inside the method, the variable builder does not point to the new object, and the builder points to the same object, so when builder accesses the same object in the heap, the data changes:

5. Custom Types
 public  class   Myinner { public  int   A;}  public  class   test{ public  void      change (Myinner in01) {in01.a  = 1 public  void   Testdemain () {myinner in  = new   Myinner ();        In.a  = 9;        Change (in);    System.out.println (IN.A); }}

Actual output: 1

The execution process is the same as the StringBuilder execution process, which is not mentioned here. Here is a little change to the above code, as follows:

 Public classMyinner { Public intA;} Public classtest{ Public voidChange (Myinner in01) {In01=NewMyinner ();//make in point to a new objectin01.a = 1; } @Test Public voidTestdemain () {Myinner in=NewMyinner (); In.a= 9;        Change (in);    System.out.println (IN.A); }}

Actual output: 9

when the parameter pass is complete, in01 and in point to the same object, In01 the operation of the object is equivalent to the in object operation, and then executes "in01=new myinner () inside the method;", so In01 it points to a new object, in01 all operations are irrelevant to in:

Combined with the above operation results and analysis, we know that the Java parameter delivery method conforms to the value of the transfer.

three-Quote delivery1. What is reference delivery?

The memory address of the variable itself is passed into the method, the variable in the method points to a variable outside the method, and the operation of the variable in the method is the outer variable of the method. the Operation .

2. Custom Types
 Public classMyinner { Public intA;} Public classtest{ Public voidChange (Myinner in01) {In01=NewMyinner ();//make in point to a new objectin01.a = 1; } @Test Public voidTestdemain () {Myinner in=NewMyinner (); In.a= 9;        Change (in);    System.out.println (IN.A); }}

Actual output: 9

If a reference is passed, after the pass is completed, In01 points to in, the operation of In01 is the operation of in, In01 points to object 2, then in also points to object 2, Output 1, and does not conform to the actual, so it is not used to pass the reference.

No more analysis of other variable types, analysis can be found that Java in passing parameters in the use of the is not a reference pass, but a value pass.

Simply put, when a value is passed, both inside and outside the method are two variables that have the same pointer, and the reference is passed with the same variable inside and outside the method.

Reference:

http://blog.csdn.net/u013366812/article/details/52062178
Http://www.cnblogs.com/buywine/p/6420555.html
Http://www.cnblogs.com/clara/archive/2011/09/17/2179493.html
Http://blog.sina.com.cn/s/blog_4df91b180100nozy.html

Values passed by the Java parameter Pass

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.