Questions about whether a Java object is passed as a parameter or as a pass-through reference

Source: Internet
Author: User
Tags stringbuffer

Objective

In Java, it is a controversial topic when an object is passed as a parameter, whether it is passing the object's value or the object's reference. If a value is passed, then the function receives only one copy of the argument, and the function's operation on the parameter does not affect the argument, and if the reference is passed, then the operation of the parameter will affect the argument.
First, let's take a look at the code:

New Object ();

Create an object, create a reference named Obj, and let the reference point to the object, as shown in:

After we have the above basis, we will take a look at the following group of popular online examples:

The base data type is passed as a parameter:

Example 1:

 Public class Test {    publicstaticvoid  main (string[] args) {        int i = 1 ;        System.out.println ("before change, I =" +i);        Change (i);        System.out.println ("After change, i =" +i);    }      Public Static void Change (int  i) {        = 5;    }}

This example is not difficult to understand, when the basic data type (boolean,byte,char,string,int,long,float,double) is passed as a parameter, the pass is a copy of the value of the argument, that is, the value is passed, no matter how to manipulate the copy in the function, The value of the argument is not changed . So the result of the above code execution is:

Before change, I = 1

The object is passed as a parameter:

In the following example 2, we pass the StringBuffer object as a parameter to the change function.

 Example 2:

 public  class   Test { public  static  void   main (string[] args) {stringbuffer sb  = new StringBuffer ("Hello"  before change, SB-is "+sb.tostring ());        Change (SB);    System.out.println ( after change, SB-is "+sb.tostring ());  public  static  void   change (StringBuffer stringbuffer) {stringbuffer.append (" World! "    

In order to facilitate the inference of the conclusion, we first look directly at the operation of the program results:


From the output we can see that the value of the object pointed to by SB has been changed, then whether we can infer that in Java, when the object is passed as a parameter, the object is passed a reference? Let's look at the following example:

 Public classTest { Public Static voidMain (string[] args) {StringBuffer sb=NewStringBuffer ("Hello"); System.out.println ("Before change, SB-is" +sb.tostring ());        Change (SB); System.out.println ("After change, SB-is" +sb.tostring ()); }     Public Static voidChange (StringBuffer stringbuffer) {StringBuffer=NewStringBuffer ("Hi"); Stringbuffer.append ("World!"); }}

If the above inference is correct, that is, the object in Java is passed as a parameter, actually passing the object's reference, then after calling the change function, the value of the original object should be changed to "Hi world!" ", however, when we run the program, the result is as follows:


The value of the original object has not been changed, which contradicts the above inference! Why in Java, when an object is passed as a parameter, sometimes the arguments are changed, and sometimes the arguments are not changed? Let's examine the reasons for this:
From the beginning of the article we know that when executing stringbuffer SB = new StringBuffer ("Hello"), we create a reference "SB" to the new object "New StringBuffer (" Hello "), as shown in:

In Example 2, when we call the change function, in fact, the formal parameter stringbuffer also points to the object that the argument SB points to, namely:

So when we execute Stringbuffer.append ("World!") , the object's value is modified by the object's reference "StringBuffer", making it "Hello world!" ", namely:

However, in the change function in Example 3, we created a new object "New StringBuffer (" Hi ")" (which actually opens up a new area in memory outside of the original object's address), which allows the formal parameter stringbuffer to actually point to the newly created object. and set the value of the new object to "Hi world!" ", the value of StringBuffer in example three has been changed for Hi World, but because the value is not returned by return assignment to SB object, so the SB object is not changed, so the output is still hello, namely:

It is not difficult to understand why the value of the argument is still "Hello" after the change function has been executed.
  

Conclusion

  In summary, we can conclude that in Java, when an object is passed as a parameter, it actually passes a copy of the reference. (a reference to the object is actually passed)

Reprinted from: 52454479

Questions about whether a Java object is passed as a parameter or as a pass-through reference

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.