Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8574701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:31:58+00:00 2026-06-11T19:31:58+00:00

Please pardon the beginner question.I met this code fragment on the internet: public class

  • 0

Please pardon the beginner question.I met this code fragment on the internet:

public class Person {

public static void main(String [] args)
{
    StringBuffer a = new StringBuffer("A");
    StringBuffer b = new StringBuffer("B");
    operate(a,b);
    System.out.println(a+","+b);
}


static void operate(StringBuffer x, StringBuffer y)
{
    y.append(x);
    y=x;

 }
}

I figured out the running output should be A,A, however, the correct output should be A,BA, could expert help me understand why the value of b is still AB? Why “y.append(x)” will affect the value of b, but not “y=x”? That is where I am getting confused.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:31:59+00:00Added an answer on June 11, 2026 at 7:31 pm

    The short answer is that Java arguments are passed by value, so the final assignment in operate doesn’t have any effect.

    The detailed sequence of events is as follows:

    1. a and b are initialized to two StringBuffer objects. Lets write this as

       a -> S1{"A"}
       b -> S2("B")
      
    2. The (trivial) argument expressions for the operator call arguments are evaluated giving S1{“A”} and S2{“B”}.

    3. The call starts, and the two references are assigned to the local variables x and y. So the state is now:

       a -> S1{"A"}
       x -> |
      
       b -> S2("B")
       y -> |
      
    4. The y.append(x) call modifies the S2 object:

       a -> S1{"A"}
       x -> |
      
       b -> S2("BA")
       y -> |
      
    5. The y = x; assignment is performed:

       a -> S1{"A"}
       x -> |
       y -> |
      
       b -> S2("BA")
      
    6. The operate method returns, causing x and y to go out of scope.

       a -> S1{"A"}
       b -> S2("BA")
      

    The key thing to note is that in step 5 we DID NOT change the contents of the S2 object. Instead, we just changed y to refer to the other object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please pardon me for a n00bish question. Please consider the following code: public class
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
Perhaps this is a very basic question, please pardon me if it is ---
I am an iOS newbie, so please pardon me if this is a beginner
Please pardon the naivety of this question. It's 2:00 AM. In doing some performance
I'm completely new to Simple Build Tool, so please pardon my beginner question. I'm
NOTE: Updated and rewritten This question has been redone and updated. Please pardon outdated
I have the following code: (please pardon the length of this code). I am
Please i need help, this code below works fine on my localhost, php5.3+ but
Please pardon me if this has been asked before, but I'm wondering if there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.