5 Replies Latest reply: Jul 16, 2007 8:53 AM by 807600 RSS

    different Result From String and String Buffer

    807600
      I am getting differnet result for String and StringBuffer case.
      public class NewClass {
              
              public static void main(String arg[]){
                      StringBuffer sb1 = new StringBuffer("A");
                      StringBuffer sb2 = new StringBuffer("B");
                      sb1.append(sb2);
                      System.out.println("sb1 : "+sb1+" sb2 : "+sb2);//O/P is sb1 : AB sb2: B
                      
                      String s1 = new String("A");
                      String s2 = new String("B");
                      operateString(s1,s2);
                      System.out.println("s1 : "+s1+" s2 : "+s2);O/P is s1 : A s2: B
              }
           private static void operate(StringBuffer x, StringBuffer y) {
                      x.append(y);
                      y = x;
             }
              private static void operateString(String x, String y) {
                      x.concat(y);
                      y = x;
              }
              
              
      }
      while i call append method of StringBuffer what is happening internally?
      I wants to know like what happened to the Content of StringBuffer sb1, how it is changing from A to AB.
      Pls. guide me to understand this point