The so-called value transfer and reference in Java

Source: Internet
Author: User

Http://www.bccn.net/Article/kfyy/java/jszl/200601/3069.html

 

My personal understanding is as follows:

 

1. For some basic types, it is to pass the value, "Class Object", and to pass the reference;

2. Pass the referenced parameter, public void change (stringbuffer SB ){

 

// Scenario 1:

SB. append ("I changed ");

 

// Scenario 2:

SB = new stringbuffer ("I am a new declared space ");

SB. append ("I changed ");

}

 

Public static void main (string [] ARGs ){

 

Test. testchange ();

Stringbuffer B = new stringbuffer ("My original status ");

System. Out. println ("Before change" + B );

Test. Change (B );

System. Out. println ("changed:" + B );

 

}

 

Running result:

 

My original status before change

After the change: My original status changed

 

B In change (B) is actually a reference pointing to B. in Case 1, the reference is the same as B, so it is changed,

Case 2: The reference is changed, and B is no longer the same reference, so how to change Sb has nothing to do with it.

 

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.