Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace StringBuffer/StringBuilder by String [duplicate]

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");
like image 702
Zehafta Tekea Avatar asked Feb 08 '23 09:02

Zehafta Tekea


2 Answers

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);
like image 158
Peter Lawrey Avatar answered Feb 15 '23 09:02

Peter Lawrey


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.

like image 26
rdonuk Avatar answered Feb 15 '23 09:02

rdonuk