Empty
String
constructor is inefficient, use ""
instead35 public static void main(String[] args) throws IOException {
36 File configLocation = new File(args[1]); // JAVA-E0406
37 BufferedReader configReader = null;
38 CharBuffer configBuf = CharBuffer.wrap(new String());39
40 HashMap<String, BigDecimal> hm = new HashMap<>();
41
Description
Creating a new java.lang.String
object using the default constructor wastes memory because the object so created will be functionally indistinguishable from the empty string constant ""
.
Java guarantees that identical string constants will be represented by the same String
object. Therefore, you should just use the empty string constant directly.
Bad Practice
String a = new String("");
Recommended
String a = "";
References
- Spotbugs - DM_STRING_VOID_CTOR