1

I am checking the performance of the code, I have "".equals(string) in java string value comparison but i have got information like string.trim().length() == 0 gives better performance. Is it true? I have got doubt about this because "".equals(string) will take care even if string is null but for the second thing first we need to null check then only we can trim the string.

which one is better to use: if("".equals(string)) or if(string.trim().length() == 0).

6
  • 6
    these 2 are different Commented Nov 6, 2014 at 6:08
  • how those can be different? Commented Nov 6, 2014 at 6:10
  • 1
    one trims the string, one does not. One returns true for " ", the other returns false. Commented Nov 6, 2014 at 6:10
  • Please look into edited question, in both the cases true or false will be return Commented Nov 6, 2014 at 6:12
  • 1
    "Premature optimization is the root of all evil" - Donald Knuth Commented Nov 6, 2014 at 6:36

2 Answers 2

4

string.trim().length() == 0 is not the same result as "".equals(string). There is no sense in comparing their performance because they serve different purposes. (However, for the purpose of speculation string.trim().length() == 0 will generally be slower because trim involves an array copy.)

  • string.trim().length() == 0 means "string might not be empty but only contains whitespace".
  • "".equals(string) means "string is only empty or null".

Just for example "".equals(" ") evaluates to false but " ".trim().length() == 0 evaluates to true.

Now the results of the expressions "".equals(string) and string != null && string.length() == 0 are the same. string != null && string.length() == 0 will technically be faster but it really doesn't matter. The difference will be so small, on the scale of a few nanoseconds. This is because "".equals(string) will only get as far as comparing the lengths.

Also in general this is not the type of thing to worry about optimizing.

Sign up to request clarification or add additional context in comments.

4 Comments

@majji One is simply not better than another. These expressions have different meanings. If you don't want to throw a NPE you can just as soon as do string != null && string.trim().length() == 0 but this has different meaning from "".equals(string).
Thank you @Radiodef, that's a tool report,here we have to follow the suugestions given by tool. So the tool is asking to change from "".equals(string) to string.trim().length() == 0. but it is not considering null check while calculating the performance. for saftey purpose we are adding a null check without tool suggestion
You should really ignore this tool's suggestion unless it can explain to you why changing the semantics of your code is acceptable as a substitution.
FWIW, instead of .length() == 0, I'd use .isEmpty() (available since JDK6).
0

This both approaches are different
approach 1:
if("".equals(string)) - compare your strings it return true if both string having same contents. refer java doc

boolean equals(Object anObject)
Compares this string to the specified object.

and

approach 2 :

if(string.trim().length() == 0) - compare length of string, if both string having same length then it will return true.

To handle null prointer exception, you have to first check whether string is null or not in second approach then call trim() method on string object.

2 Comments

"To handle null prointer exception, you have to first check whether string is null or not in second approach then call trim() method on string object." So what if string is null,which one is better?
approach 1 is better approach if the string is null. it will not through null pointer exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.