1
public class test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        StringBuffer a = new StringBuffer("A");
        StringBuffer b = new StringBuffer("B");

        operator(a, b);
        System.out.println(a + "," + b);

    }

    public static void operator(StringBuffer a, StringBuffer b) {
        a.append(b);
        b = a;
    }

Why is b still print out B? I did b = a in the operator, shouldn't it print out AB as well?

kegs Production
  • 67
  • 1
  • 13
RickyPang
  • 55
  • 1
  • 8
  • `b = a;` in `operator` just overwrites the value in the `b` parameter with the value of the `a` parameter. It has no effect whatsoever on the `b` local variable in `main`, which is entirely unrelated to that parameter. `operator(a, b)` takes the **values** from `a` and `b` and passes those **values** into `operator`; there's no link to the variables those values came from. – T.J. Crowder Nov 06 '17 at 11:45
  • b is a by value parameter. b=a has no effect over the original variable on the caller side. – 1010 Nov 06 '17 at 11:46

0 Answers0