creates a new sb reference variable and it refers to the object from main. When a new assignment is made in the next line, a new string buffer object is created and is assigned to sb. Thus anymore changes done to sb are done on the new object rather than the one in main.
In the second case, you are first appending the stringbuffer with a value, so here the stringbuffer object from main is changed first. sb then references to another object created by new.
We can always think of object references are passed by reference while variables are passed by value (though it is specified that arguments are just passed by value in java). Since any changes made to the variables in methods are not reflected in the actual values, while the changes made to the objects are reflected.
Joined: Jan 25, 2008
Thanks Ravikanth. I got it . [ March 04, 2008: Message edited by: sandhi mridul ]