The problem of stringbuffer passing as parameters

Source: Internet
Author: User

Public class Foo {
2. Public static void main (string [] ARGs ){
3. stringbuffer A = new stringbuffer ("A");
4. stringbuffer B = new stringbuffer ("B");
5. Operate (A, B );
6. system. Out. printin {A + "" + B };
7 .)
8. Static void operate (stringbuffer X, stringbuffer y ){
9. x. append (y );
10. Y = X;
11 .}
12 .}

 

Printed as AB B

Because: AB B
Variable A \ B \ x \ y stores references of the stringbuffer variable rather than a stringbuffer object. The parameter received by operate is only a reference of the stringbuffer object. therefore, it can be understood that X and a all point to the same object; B and Y also point to the same stringbuffer object, so X. append (y) will change the stringbuffer object that X and a refer to (Add "B"); while y = x only changes the variable Y to the stringbuffer object that is the same as X, the object originally referred to by Y does not change.

 

Therefore, stringbuffer is the address passed by reference transmission.

Although the string type is also an object, the final type is equivalent to passing the value.

The problem of stringbuffer passing as parameters

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.