String method Append() : StringBuilder vs StringBuffer

2013-06-18T23:47:17

With this code:

  public static void main(String[] args) {

    String s = "Java";
    StringBuilder buffer = new StringBuilder(s);
    change(buffer);

       System.out.println("What's strBuf.charAt(5)? " + strBuf.charAt(3));
       System.out.println(buffer);
  }

     private static void change(StringBuilder buffer) {

          buffer.append(" and HTML");
  }

When I run the code using StringBuilder I get error message

 The constructor StringBuilder(String) is undefined
    The method charAt(int) is undefined for the type StringBuilder

Tried StringBuffer instead and it works. The content of the StringBuffer object is compiled to "Java and Eclipse.."

  public static void main(String[] args) {

    String s = "Java";
    StringBuffer strbuf = new StringBuffer(s);
    change(strbuf);

               System.out.println("The Stringbuffer.charAt(5) is ? " + strbuf.charAt(3));
           System.out.println(strbuf);
 }

       private static void change(StringBuffer strbuf) {

                       strbuf.append(" and Eclipse");
 }

}

Copyright License:
Author:「z atef」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/17173314/string-method-append-stringbuilder-vs-stringbuffer

About “String method Append() : StringBuilder vs StringBuffer” questions

With this code: public static void main(String[] args) { String s = "Java"; StringBuilder buffer = new StringBuilder(s); change(buffer); System.out.println("What's strBuf.ch...
I have a StringBuffer initialized outside for loop and inside for loop I am concatenating some strings. I am getting the warning 'StringBuffer stringBuffer' may be declared as 'StringBuilder'...
I was looking at the String Javadoc when I noticed this bit about String concatenation: The Java language provides special support for the string concatenation operator ( + ), and for conversi...
The only/major difference between StringBuffer and StringBuilder is StringBuffer is thread safe and StringBuilder is not. So use StringBuilder when it is going to be accessed from a single thread ...
For the following code, which class should I use in a static method, StringBuilder or StringBuffer? Based on the API, StringBuffer is thread-safe, but StringBuilder is not. public static String
i know that in servlet filter we should watch out for thread safty of our instace varibles but in the following code , i used stringBuilder and stringBuffer (one thread safe, one not). public class
According to Netbeans hint named Use chain of .append methods instead of string concatenation Looks for string concatenation in the parameter of an invocation of the append method of StringBuild...
what is the difference between the StringBuffer vs StringBuilder Vs StringTokenizer on the internal implementation. when to use these . kindly waiting for the answer. Update:- I am also going throu...
Is there any substantial advantage in using a XMLStreamWriter: Writer out = new StringWriter(); XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(out); writer.
I'm currently doing a piece of code which must be able to match some regexp and do some replacement inside the string which match the regexp. In order to do that I'm using the matcher object in ja...

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.