I have a StringBuffer initialized outside for loop and inside for loop I am concatenating some strings.
I am getting the warning
'StringBuffer stringBuffer' may be declared as 'StringBuilder'
and
string concatenation as argument to 'stringbuilder.append()' call
Then I changed that StringBuffer to StringBuilder, since it is comparatively faster than StringBuffer. Now I am getting the warning as
string concatenation as argument to 'stringbuilder.append()' call
Sample code:
public static String stringConcat(String[] words) {
StringBuffer stringBuffer = new StringBuffer();
for (String word : words) {
stringBuffer.append(word).append(" ");
}
return stringBuffer.toString();
}
Why I am getting these warnings.
Edit Actual code:
stringBuffer.append(word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase()).append(" ");
StringBuilder
toStringBuffer
, and if you can use Java 8+ language features - I would preferreturn Stream.of(words).collect(Collectors.joining(" "));
– Coherentappend()
. Also don't post sample code that obfuscates the problem. – Paranoid