wood burning stoves 2.0*
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
Author

.equals("")

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)))
{
doSomething;
}

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(""))
{
doSomething;
}

Thank you for any comments.

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

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
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
You can avoid the NullPointerException by reversing the operands:
<pre>
if ("".equals(in_strSelectedValue))
{
doSomething();
}
</pre>
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("")