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?
Solved! Go to Solution.
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
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
hey thank you for your help