Select to view content in your preferred language

StringBuffer in java

715
2
Jump to solution
01-13-2023 04:55 PM
hectorsalamanca
Deactivated User

Hello Everyone,

Consider the following example:

 

public class Test
{
static void operate (StringBuffer x, StringBuffer y)
{
x.append(y);
y = x;
}

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

 

The output is: AB,B

I thought arguments were always supplied "by value" in Java. Meaning that copies of objects "a" and "b" would be provided to the method "operate".

However, in the above example, while object "b" behaves as anticipated (since it did not change after being sent to "operation"), object "a" has changed, which perplexes me. As if "a" was passed by reference and "b" was passed by value. Is there something I'm missing here?

 

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

Yes, you're passing values.

BUT: Your values are objects with attributes and methods. In your operate function, you're using these methods to change the internal attributes of your values.

 

Here's what's happening:

static void operate (StringBuffer x, StringBuffer y)
{
  // x and y are passed by value. but they have internal values themselves.
  // here, you're changing x's internal value. You're not changing x itself, only its internal value!
  x.append(y);

  // here, you're creating a new variable in the local scope of this function.
  // this does not change the variable you passed into this function, because
  // it is outside of this function's scope.
  // IF you would be passing by reference (which you are not!), this would
  // change the outer y
  y = x;
}


public static void main (String [] args)
{
  StringBuffer a = new StringBuffer ("A");
  // a = "A"
  StringBuffer b = new StringBuffer ("B");
  // b = "B"
  operate (a,b);
  // a = "AB", because you changed a's internal value
  // b = "B", because you didn't change its internal value
  // y_inside_the_function = "AB", but y only exists inside the function's scope
  System.out.println(a + "," +b);
  // "AB,B"
}

 

There are some good explanations in this StackOverflow post: methods - Is Java "pass-by-reference" or "pass-by-value"? - Stack Overflow


Have a great day!
Johannes

View solution in original post

2 Replies
JohannesLindner
MVP Frequent Contributor

Yes, you're passing values.

BUT: Your values are objects with attributes and methods. In your operate function, you're using these methods to change the internal attributes of your values.

 

Here's what's happening:

static void operate (StringBuffer x, StringBuffer y)
{
  // x and y are passed by value. but they have internal values themselves.
  // here, you're changing x's internal value. You're not changing x itself, only its internal value!
  x.append(y);

  // here, you're creating a new variable in the local scope of this function.
  // this does not change the variable you passed into this function, because
  // it is outside of this function's scope.
  // IF you would be passing by reference (which you are not!), this would
  // change the outer y
  y = x;
}


public static void main (String [] args)
{
  StringBuffer a = new StringBuffer ("A");
  // a = "A"
  StringBuffer b = new StringBuffer ("B");
  // b = "B"
  operate (a,b);
  // a = "AB", because you changed a's internal value
  // b = "B", because you didn't change its internal value
  // y_inside_the_function = "AB", but y only exists inside the function's scope
  System.out.println(a + "," +b);
  // "AB,B"
}

 

There are some good explanations in this StackOverflow post: methods - Is Java "pass-by-reference" or "pass-by-value"? - Stack Overflow


Have a great day!
Johannes
hectorsalamanca
Deactivated User

hey thank you for your help

 

0 Kudos