StringBuffer() to StringBuilder()


Property Value
First seen in jSparrow version 2.2.0
Minimum Java version 5
Remediation cost 2 min
Links S1149


This rule changes the type of local variables from StringBuffer() to StringBuilder(). The java.lang.StringBuilder and the java.lang.StringBuffer have exactly the same API and functionality, so it could just be replaced without any effort. The only difference is, that StringBuffer uses synchronized methods, which doesn't automatically result in thread safety. In almost all cases, it is recommended to use StringBuilder because no locking and syncing is done. Hence, it's faster. Also the java compiler optimizes strings, which are altered, and replaces them with StringBuilder.


When running programs in a single thread, which is generally the case, StringBuilder offers performance benefits over StringBuffer. This is due to the fact that StringBuffer is synchronized and StringBuilder is not. For a reference see here.

Requirement & Tags


Java 5

Code Changes


StringBuffer localStringBuffer = new StringBuffer();


StringBuilder localStringBuffer = new StringBuilder();

Automatic Application of This Rule

The automatic application of this rule is supported in the following jSparrow version: