Synopsis: | Initialize objects of StringBuffer with a proper size |
Language: | Java |
Severity Level: | 8 |
Category: | StringandStringBuffer |
Description: |
Failing to pre-size a StringBuffer properly could cause it to re-size many times
during runtime. This rule checks the characters that are actually passed into
StringBuffer.append(), but represents a best guess "worst case" scenario. An
empty StringBuffer constructor initializes the object to 16 characters. This default
is assumed if the length of the constructor can not be determined.public class Foo { void bar() { StringBuffer bad = new StringBuffer(); bad.append("This is a long string, will exceed the default 16 characters");//bad StringBuffer good = new StringBuffer(41); good.append("This is a long string, which is pre-sized");//good } } |