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

Copy
      public boolean emptyCheck1(String s) {
         if (s.equals("")) return true;
         return false;
     }
     public boolean emptyCheck2(String s) {
         if ("".equals(s)) return true;
         return false;
     }
     // fixed code
     public boolean emptyCheck3(String s) {
         if (s.length() == 0) return true;
         return false;
     }

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