On 6/2/06, Scott Van Wart <[EMAIL PROTECTED]> wrote:
If a bean's property is the empty string "", does a test like (property
== null) suffice? Or do I have to say ((property == null) or (property
== ''))?
You need to test for both conditions in the latter case, but the particular
approach you suggest above is not necessarily going to work. You cannot
reliably do "==" tests on strings if you are looking for equality -- you
need to use the equals() function instead.
When faced with the kind of test you're trying to do here, I would suggest
the following:
if ((property == null) || property.equals("")) {
... it is either null or an empty string ...
}
It is important to do the null test first, because if that test passes, the
expression evaluation rules guarantee that the application won't try to
evaluate property.equals("") as well. That's a good thing ... because that
would generate a null pointer exception if property was indeed null.
If you want to learn more about Java basics things like this, I would
strongly recommend working your way through the Java Language Tutorial[1].
It has concise introduction to language concepts like this, plus it goes
over the basics of using many of the common Java APIs.
- Scott
Craig
[1] http://java.sun.com/docs/books/tutorial/