ESCMP.EMPTYSTR

ESCMP Compare string with an empty string using equals(). It is not necessary to call equals() to compare a string with an empty string. s.length() works twice as fast. The following expressions: s.equals("") or "".equals(s) can be easily replaced with (s.length() == 0) and (s != null && s.length() == 0) Performance measurements (done using Java 2 Runtime Environment, Standard Edition, build 1.4.1_02-b06) showed that code with "equals" executed in 147 units of time while the same code with "length" executed in 71 units of time.

Example 1

9      public boolean emptyCheck1(String s) {
10         if (s.equals("")) return true;
11         return false;
12     }
13     public boolean emptyCheck2(String s) {
14         if ("".equals(s)) return true;
15         return false;
16     }
17     // fixed code
18     public boolean emptyCheck3(String s) {
19         if (s.length() == 0) return true;
20         return false;
21     }

ESCMP.EMPTYSTR is reported for line 10: Comparing strings 's' and "" using equals(), instead of length() == 0 ESCMP.EMPTYSTR is reported for line 14: Comparing strings "" and 's' using equals(), instead of length() == 0