can someone explain this behavior of StringBuffer? -
can someone explain this behavior of StringBuffer? -
public class strbuffer { public static void main(string[] args) { stringbuffer sb = new stringbuffer(); //5 sb.append("hello"); //6 foo(sb); //7 system.out.println(sb); //8 } private static void foo(stringbuffer sb) { // todo auto-generated method stub sb.append("wow"); //1 sb = new stringbuffer(); //2 sb.append("foo"); //3 system.out.println(sb); //4 } }
in above when print in line8. output "hellowow" .... can 1 explain please?
sb.append("wow"); //1
you mutated stringbuffer
instance passed in.
sb = new stringbuffer(); //2
you assigned local parameter point new stringbuffer
instance. has no effect on caller or on old instance.
stringbuffer
Comments
Post a Comment