Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using String or StringBuffer in Java: which is better?

Tags:

java

string

I read a lot about using StringBuffer and String especially where concatenation is concerned in Java and whether one is thread safe or not.

So, in various Java methods, which should be used?

For example, in a PreparedStatement, should query be a StringBuffer:

    String query = ("SELECT * " +
                    "FROM User " +
                    "WHERE userName = ?;");

    try {
        ps = connection.prepareStatement(query);

And then again, in a String utility methods like:

public static String prefixApostrophesWithBackslash(String stringIn) {
    String stringOut = stringIn.replaceAll("'", "\\\\'");
    return stringOut;
}

And:

 // Removes a char from a String.
public static String removeChar(String stringIn, char c) {
    String stringOut = ("");
    for (int i = 0; i < stringIn.length(); i++) {
        if (stringIn.charAt(i) != c) {
            stringOut += stringIn.charAt(i);
        }
    }
    return stringOut;
}

Should I be using StringBuffers? Especially where repalceAll is not available for such objects anyway.

Thanks

Mr Morgan.

Thanks for all the advice. StringBuffers have been replaced with StringBuilders and Strings replaced with StringBuilders where I've thought it best.

like image 942
Mr Morgan Avatar asked Jul 19 '10 07:07

Mr Morgan


People also ask

Which is better String or StringBuffer?

The StringBuffer class is used to represent characters that can be modified. The significant performance difference between these two classes is that StringBuffer is faster than String when performing simple concatenations.

Why StringBuffer is faster than String?

String is immutable, if you try to alter their values, another object gets created, whereas StringBuffer is mutable so they can change their values. Thats why StringBuffer is faster than String.

What is the advantage of StringBuffer over String class?

StringBuffer uses less memory as compared to the string. It utilises a string constant pool to store the values. It prefers heap memory to store the objects. It overrides both equal() and hashcode() techniques of object class.

Which is faster String or StringBuilder in Java?

StringBuilder executes significantly faster than the String class when performing the concatenation or modification operations. Modifying a String creates a new String in the heap memory. To change the content of the String, we should consider the StringBuilder class.


1 Answers

You almost never need to use StringBuffer.

Instead of StringBuffer you probably mean StringBuilder. A StringBuffer is like a StringBuilder except that it also offers thread safety. This thread safety is rarely needed in practice and will just cause your code to run more slowly.

Your question doesn't seem to be about String vs StringBuffer, but about using built-in methods or implementing the code yourself. If there is a built-in method that does exactly what you want, you should probably use it. The chances are it is much better optimized than the code you would write.

like image 139
Mark Byers Avatar answered Nov 14 '22 20:11

Mark Byers