Between String and StringBuffer Classes which class executes faster?
Printable View
Between String and StringBuffer Classes which class executes faster?
Executes what? If you are doing a lot of concatenation, go with a stringbuffer. It doesn't reference new memory every time you reassign it like a string does.
For concatenation i would think that using a StringBuffer right from the start would allow your code to execute faster since the compiler uses StringBuffers to implement the string concatenation operator.
String s = "Hello." + "What" + "time" + "is" + "it?";
// is equivalent to......
String s2 = new StringBuffer().append("Hello").append("What").append("time").append("is").append("it?").toString();
Java 1.5 has a new class, StringBuilder, which works exactly like StringBuffer but is not thread-safe and thus faster.