Forums Register Login

.equals("")

+Pie Number of slices to send: Send
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).]
+Pie Number of slices to send: Send
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).]
+Pie Number of slices to send: Send
Thank you.
+Pie Number of slices to send: Send
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'd appreciate it if you pronounced my name correctly. Pinhead, with a silent "H". Petite ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 560 times.
Similar Threads
passed with 96%
Leafs sweep Sens ...
Classpath for OOP-1 DaysOld
Cattle Drive Hall of Fame
please help, class casting problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:35:57.