This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
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 ]