StringBuffer() to StringBuilder()

Properties

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

Description

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.

Benefits

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

Requirements

Java 5

Code Changes

Pre

StringBuffer localStringBuffer = new StringBuffer();

Post

StringBuilder localStringBuffer = new StringBuilder();