satellite03
IS-IT--Management
Code:
public class TestBuffer {
public void myBuf( StringBuffer s, StringBuffer s1) {
s.append(" how are you") ;
s = s1; //[COLOR=blue] question 1 [/color]
}
public static void main ( String args[] ) {
TestBuffer tb = new TestBuffer();
StringBuffer s = new StringBuffer("Hello");
StringBuffer s1 = new StringBuffer("doing");
tb.myBuf(s, s1);
System.out.print(s);
}
}
output of this code > Hello how are you
question 1 > why the output is not "doing" ?
look the assignmnet s = s1 . it means whatever s1 is pointing (in this case "doing" ), the same thing will be pointed to by s now.
now print(s)
so, the output must be " doing " .
bcoz assignment of refernces means they will refer to the same chunk of memory and in this case that will be "doing" due to s=s1
whats wrong with this code ? how the output is "Hello how are you " ? it should have been "doing".
can you tell on which logic it is showing that output ?