The best way to use the .equals() method when comparing values of String objects is to write it as:
"Hello World".equals(fooBar);
"".equals(fooBar);
"".equals(fooBar);
rather than:
fooBar.equals("Hello World");
fooBar.equals("");
fooBar.equals("");
Both sets of examples will compile but the first set guarantees that a NullPointerException is never thrown when you perform the comparison. The second set may risk throwing a NullPointerException if the object fooBar happens to be null.
No comments:
Post a Comment