When to use String, StringBuilder and StringBuffer in Java

Any programming language developer(s) should always tend to write code which is of very less impact on performance management of the application. But, most of the developers fail many a times to achieve the same, especially when working with string related operations (string concatenation) in the code which is to be written. So every java developers are aware that a String object can be created using String/StringBuffer/StringBuilder.

In this tips & tricks article, we would learn on how to use the above String classes in order to write an effective code without causing any performance issues. In fact, this may seems for a beginners level, but, would strongly recommend that every java developer should follow the below tips.

String class

Let us consider that I need to construct a simple SQL statement

 INSERT INTO STUDENT (regNo, name, course, address) values (….);

Consider that we are using String class to construct the same as shown below:

String insertSql = "INSERT INTO STUDENT (regNo, name, course, address)";
insertSql += "values (" + student.getRegNo();
insertSql += "', '" + student.getName();
insertSql += "', '" + student.getCourse();
insertSql += "', '" + student.getAddress();
insertSql += "')";

But, there is a problem with the above code snippet. It is a known thing in java that the String class is immutable !! i.e. once a string object is created, then it cannot be changed. Which means, in the first line of the above code snippet

 String insertSql = "INSERT INTO STUDENT (regNo, name, course, address)"

one object of the string is created. When the string concatenation is to be made, then a new reference in the memory should be provided. Hence, when the line

 insertSql += "values ("

is created, 2 new objects will be created by occupying new memory which leads to the performance issues obviously!

This sounds really bad when it comes to performance issue while programming. So, let us see how we overcome this performance issue.

StringBuilder pitches in as a rescue ranger!! In java, the StringBuilder is designed to be mutable and helps developers not to create any performance issues while working on string concatenation operation. We will see how the above code snippet can be re-written using the StringBuilder class.


StringBuilder insertSQLSb = new StringBuilder();
insertSQLSb.append("INSERT INTO STUDENT (regNo, name, course, address)");
insertSQLSb.append("values (" + student.getRegNo());
insertSQLSb.append("', '" + student.getName());
insertSQLSb.append("', '" + student.getCourse());
insertSQLSb.append("', '" + student.getAddress());
insertSQLSb.append("')");

Hence, every developer has to know that StringBuilder class should be used to concatenate bigger strings to gain good performance.

Why not to try with StringBuffer class in java?

StringBuffer class in java is same as the StringBuilder, except that the methods of the StringBuffer are not synchronized, whereas the methods in the StringBuilder class are synchronized. So, this signifies that the StringBuilder should be used only in a multi-threaded context of the application. The above code snippet can be re-written using StringBuffer as shown below:

StringBuffer insertSQLSb = new StringBuffer();
insertSQLSb.append("INSERT INTO STUDENT (regNo, name, course, address)");
insertSQLSb.append("values (" + student.getRegNo());
insertSQLSb.append("', '" + student.getName());
insertSQLSb.append("', '" + student.getCourse());
insertSQLSb.append("', '" + student.getAddress());
insertSQLSb.append("')");

Please follow and like us:

Questions? Comments? Suggestions? Let us know!! Like / Subscribe / Follow for more updates.