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?