File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Buffer

 
RajeshParab
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 public class StrBufTest {
2 public void method1(StringBuffer s1, StringBuffer s2){
3 s1.append("There");
4 s2 = s1;
5 }
public static void main(String[] args){
StringBuffer sb1 = new StringBuffer("Hello");
StringBuffer sb2 = new StringBuffer("Hello");
StrBufTest sbt = new StrBufTest();
sbt.method1(sb1, sb2);
System.out.println("sb1 is " + sb1 + "\nsb2 is " + sb2);
}
}
Answer is: sb1 is "HelloThere"; sb2 is "Hello".
When we say s2 = s1. It creates copy of reference. both should have save content. How come sb1 and sb2 is different???
 
josephine chen
Ranch Hand
Posts: 216
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 public class StrBufTest {
2 public void method1(StringBuffer s1, StringBuffer s2){
3 s1.append("There"); //
here u are manipulating on the string directly
and so u see the change
4 s2 = s1;// u are changing the ref of the local s2 object
in method argument
5 }
public static void main(String[] args){
StringBuffer sb1 = new StringBuffer("Hello");
StringBuffer sb2 = new StringBuffer("Hello");
StrBufTest sbt = new StrBufTest();
sbt.method1(sb1, sb2);
System.out.println("sb1 is " + sb1 + "\nsb2 is " + sb2);
}
}
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
can somebody throw more light, i dont get it
 
maha anna
Ranch Hand
Posts: 1467
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please refer to this dicussion and let us know if you are convinced.
regds
maha anna
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic