I’ve been taking a look at some GWT code written by various people and there are different ways of comparing strings. I’m curious if this is just a style choice, or if one is more optimized than another:
"".equals(myString);
myString.equals("");
myString.isEmpty();
Is there a difference?
will not throw a
NullPointerExceptionifmyStringis null. That is why a lot of developers use this form.is the best way if
myStringis never null, because it explains what is going on. The compiler may optimize this ormyString.equals(""), so it is more of a style choice.isEmpty()shows your intent better thanequals(""), so it is generally preferred.