• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

.equals("")

 
Melinda Wilson
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic