The wrapper class for the "Java Foundation" base type is passed as a parameter whether it is a value pass or a reference

Source: Internet
Author: User

Suddenly think of this problem, and then do the next experiment, below with an integer to explain, the other ignored:

ImportJava.util.Iterator;/*** Created by Lili on 15/9/24.*/ Public classTestnew { Public Static voidMain (String args[]) {Integer I1= 10; Integer I2= 20; System.out.println (I1+ "   " +i2);        Change (I1,I2); System.out.println (I1+ "   " +i2); String S1=NewString ("S1"); String S2=NewString ("S2"); System.out.println (S1+ "   " +S2);        Change (s1, S2); System.out.println (S1+ "   " +S2); StringBuilder SB1=NewStringBuilder ("SB1"); StringBuilder SB2=NewStringBuilder ("SB2"); System.out.println (SB1+ "   " +SB2);        Change (SB1,SB2); System.out.println (SB1+ "   " +SB2); }     Public Static voidChange (integer i1, integer i2) {i1= 100; I2= 200; }     Public Static voidChange (String i1, String i2) {I1= "II1"; I2= "I22"; }     Public Static voidChange (StringBuilder i1, StringBuilder i2) {i1.append ("SBSBSB1"); I2.append ("SBSBSB2"); }}

Operation Result:

Ten   s1   s2s1   s2sb1   sb2sb1sbsbsb1   0

It turns out that only StringBuilder is a reference pass, and the other is the value passed.

According to common sense, should be passed the address of the object, is this because of the automatic boxing and unpacking, the compiler to help you do this result in the final value is passed?

Take this problem first to see the class file's anti-compilation code:

////Source code recreated from a. class file by IntelliJ idea//(Powered by Fernflower Decompiler)// Public classTestnew { Publictestnew () {} Public Static voidMain (string[] var0) {Integer var1= Integer.valueof (10); Integer var2= Integer.valueof (20); System.out.println (var1+ "   " +var2);        Change (var1, var2); System.out.println (var1+ "   " +var2); String Var3=NewString ("S1"); String VAR4=NewString ("S2"); System.out.println (Var3+ "   " +VAR4);        Change (VAR3, VAR4); System.out.println (Var3+ "   " +VAR4); StringBuilder VAR5=NewStringBuilder ("SB1"); StringBuilder VAR6=NewStringBuilder ("SB2"); System.out.println (VAR5+ "   " +VAR6);        Change (VAR5, VAR6); System.out.println (VAR5+ "   " +VAR6); }     Public Static voidChange (integer var0, integer var1) {var0= integer.valueof (100); Var1= Integer.valueof (200); }     Public Static voidChange (String var0, String var1) {var0= "II1"; Var1= "I22"; }     Public Static voidChange (StringBuilder var0, StringBuilder var1) {var0.append ("SBSBSB1"); Var1.append ("SBSBSB2"); }}

The source code shows that there is no optimization for value passing, because if you do, the parameters in the change should be changed to int, so that the integer object is automatically disassembled to int for value delivery, and an example of an automatic loading and unloading unpacking code is as follows:

Integer integer = one+ =9/*  integer integer = integer.valueof (11);//auto-boxing integer = integer. ValueOf ((Integer.intvalue () + 9));//automatic unpacking and crating   * /

What is the reason for that?

With this problem, the program is debug, to see if the delivery is the object address.

After entering the change method, look at the address changes

Find incoming address value, change parameter of I1 and I2 object pointing to [email protected] and [email protected], indicating the same object that points to and passed in the parameter, the incoming address.

Then execute the method change program:

When performing a change to the value of the I1, at this point I1 the object to the [email protected], this can fully explain the problem is not automatic boxing and unpacking, but the reason, my understanding is related to this, because the implementation of this step is I1 = 100; But the compiler optimized it for automatic boxing, var0 = integer.valueof (100), so i1 points to the newly created object integer.valueof (100), so the result is the same as the value passed. The value of the parameter that called the method is not changed.

The wrapper class for the "Java Foundation" base type is passed as a parameter whether it is a value pass or a reference

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.