Why does NetBeans suggest that I replace StringBuffer
/ StringBuilder
by String
?
It shows me a warning message when I write:
StringBuilder demo = new StringBuilder("Hello");
Instead of writing
StringBuilder demo = new StringBuilder("Hello");
this is simpler and you don't have to allocate a StringBuilder for it:
String demo = "Hello";
If you use a method which means it has to be a StringBuilder, the warning should go away.
e.g.
StringBuilder demo = new StringBuilder("Hello");
demo.append(" World ");
demo.append(128);
Replace StringBuffer/StringBuilder by String
The hint will find and offer to replace instances of StringBuffer or StringBuilder which are accessed using ordinary String methods and are never passed out of the method, or assigned to another variable. Keeping such data in StringBuffer/Builder is pointless, and String would be more efficient.
From netbeans wiki.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With