Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

StringBuffer

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question about the length() method. Please read the code below.

public class BuffString{
public static void main(String args[]){
StringBuffer s1 = new StringBuffer("12345");
StringBuffer s2 = new StringBuffer("abcde");
StringBuffer s3 = s1.append(s2);
System.out.println(s1);
}
}

//output

12345abcde

I don't understand why s1 should contain the addition of s1 and s2? Is it impossible to keep the original value for s1? Why can't we have three different StringBuffers with differrent values? Why is the rule this way?
Please someone explain about this so I shut up and stop whining.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the Javadocs, the append method of StringBuffer when called with a StringBuffer will append the parameter to the caller of the method.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
s1.append(s2) appends the contents of StringBuffer s2 onto the contents of s1. It changes s1's state. The method also returns a StringBuffer reference referencing the target of the method. That's what you are assigning to s3. So s3 refers to s1's StringBuffer.

It works this way to enable the chaining of method calls, for example:
String s = new StringBuffer().append("abc").append("cde").append("fgh").toString()
[ March 14, 2006: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apparently, you are confusing Strings and StringBuffers. StringBuffers are mutable - their values can be changed by using the append() function. However, Strings are immutable. If S1, S2 and S3 were Strings, and you used S3 = S1 + S2, S1 will remain the same. It is immutable (the same as if you used S1.toUpperCase(); S1 will still not change).

[ March 14, 2006: Message edited by: Kotto Bass ]
[ March 14, 2006: Message edited by: Kotto Bass ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic