jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes .equals( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ".equals("")" Watch ".equals("")" New topic


Melinda Wilson
Ranch Hand

Joined: Jul 05, 2001
Posts: 35
Please look at this code, I think it looks funny.

if ("".equals(StringUtility.blanknull(in_strSelectedValue)))

The blanknull(String) return this this:

return((in_string == null) ? "" : in_string);

Wouldn't writing it like this do the samething as the first if statement?:
if (in_strSelectedValue.equals(""))

Thank you for any comments.

[This message has been edited by Melinda Wilson (edited October 25, 2001).]
Iain Lowe

Joined: Sep 07, 2001
Posts: 12
The second code snippet will do the same as the first but if in_strSelectedValue is null your second snippet will throw a NullPointerException at runtime.
[This message has been edited by Iain Lowe (edited October 25, 2001).]
Melinda Wilson
Ranch Hand

Joined: Jul 05, 2001
Posts: 35
Thank you.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
You can avoid the NullPointerException by reversing the operands:
if ("".equals(in_strSelectedValue))
However this is still different in effect from the original code. This will execute doSomething() if the string is blank ("") but not if it is null. The original code was written to treat a null exactly the same as a blank. It's up to you to decide if this is important for your application, and which version makes more sense for you.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link: http://aspose.com/file-tools
subject: .equals("")
It's not a secret anymore!