File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Buffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Buffer" Watch "String Buffer" New topic
Author

String Buffer

RajeshParab
Greenhorn

Joined: Feb 08, 2000
Posts: 14
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

Joined: Oct 29, 2002
Posts: 216
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

Joined: Nov 22, 2008
Posts: 18944
can somebody throw more light, i dont get it
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Please refer to this dicussion and let us know if you are convinced.
regds
maha anna
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Buffer