Search the web
Sign In
New User? Sign Up
Java_Official · Java SE . EE . ME . AJAX . Web services
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Real people. Real stories. See how Yahoo! Groups impacts members worldwide.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
StringBuffer Argment - doubt   Message List  
Reply | Forward Message #31766 of 32216 |
Hi Folks,

Could you all please let me know why, the contents of stringbuffer "S1"  printed
in line 3 (//3)and line 4(//4) are not same, even though the s2 has been
assigned to s1 in line 2.
I am a bit confused with this "parsing of strings/stringbuffers" , could you
please explain me.

And how come, the value assigned to S1 in "line 2" is not going to "line 4"

Thanks a lot.

class TestBuffer {

     public void myBuf( StringBuffer s1, StringBuffer s2) {
            s1.append(" how are you") ;  // 1
            s1 = s2;  // 2
            System.out.println("Inside myBuf method: " + s1);  //3
     }

     public static void main ( String args[] ) {
             TestBuffer tb = new TestBuffer();
              StringBuffer s1 = new StringBuffer("Hello");
              StringBuffer s2 = new StringBuffer("doing");
              tb.myBuf(s1, s2);
              System.out.print(s1); //4
      }
}




Best Regards & Good Luck



Chooti Baba




[Non-text portions of this message have been removed]




Wed Dec 3, 2008 4:46 am

nibm963
Offline Offline
Send Email Send Email

Forward
Message #31766 of 32216 |
Expand Messages Author Sort by Date

Hi Folks, Could you all please let me know why, the contents of stringbuffer "S1"  printed in line 3 (//3)and line 4(//4) are not same, even though the s2 has...
Chooti Baba
nibm963
Offline Send Email
Dec 4, 2008
1:44 am

Hi, Chooti. The problem with your code is that s1 and s2 inside myBuf ARE NOT the same variables of s1 and s2 outside myBuf, they are local variables ...
Rodrigo Coin Curvo
rodrigoccurvo
Offline Send Email
Dec 5, 2008
1:23 am

Consider this line: StringBuffer s1 = new StringBuffer("Hello"); When you create any object such as new StringBuffer("Hello") Java builds the object in a...
harrybeezhold@...
harrybeezhold
Offline Send Email
Dec 5, 2008
1:23 am

hi,    answer of your queries are simple. actually StringBuffer works on same object (when we r performing any string operation). But String class creates...
Amit Kulshrestha
amitkulu2000in
Offline Send Email
Dec 5, 2008
1:47 am
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help