Yes, Java is always pass-by-value, but you need to be aware of what the value is. For objects, the value passed is the reference to the object, not the object itself. See Is Java "pass-by-reference" or "pass-by-value"? for a detailed explanation.
i am not sure why this is appending everything in a not in b.
Because this:
x.append(y);
y = x.append("C");
y.append("D");
is exactly the same as this:
x.append(y);
x.append("C");
x.append("D");
Since StringBuilder#append()
returns this
, so that assignment to y
in
y = x.append("C");
overwrites the reference to the object which was passed in (b
) with a reference to x
(aka a
).