Hi,
Will an if-statement always break at the first
occurence of a 'true' in a chained or-statement?
Like this
if( myArray == null || myArray.length() == 0 )
// Do something
Can I be sure not to have a nullPointerException
at 'myArray.length() == 0' if myArray now happened
to be null?
If 'if' is standardized and anyone has a link that
explains that, that would be terrific for me to
put in as a comment where I use such line.
Thanks
Daniel Marcus
Jacques-Olivier Haenni - 16 Jun 2005 07:31 GMT
Hi,
This is the specified behaviour of the operator ||, not of the if-statement.
Look at the Java Language Specification:
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.24
It clearly says that " The |||| operator is like ||| (§15.22.2)
<http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#5242>,
but evaluates its right-hand operand only if the value of its left-hand
operand is |false|."
Cheers,
Jacques-Olivier
> Hi,
>
[quoted text clipped - 16 lines]
> Thanks
> Daniel Marcus
Roland - 16 Jun 2005 07:31 GMT
> Hi,
>
[quoted text clipped - 16 lines]
> Thanks
> Daniel Marcus
The 'if' statement is 'standardized', it has well defined behavior.
But what you are referring to, is the result of the || operator, i.e.
the conditional OR operator. The conditional OR doesn't evaluate its
right-hand expression when the left-hand expression is true (i.e.
evaluated to true).
However, the boolean OR operator, the | operator, does evaluate both
left- and right-hand expressions.
In the following code snippet, the first if statement completes
normally, but the second if, containing the | operator, throws a null
pointer exception.
String myString = null;
if (myString == null || myString.length() == 0)
{
System.out.println("Either myString was null, "
+ "or myString was an empty String");
}
if (myString == null | myString.length() == 0)
{
System.out.println("myString was null "
+ "or myString was an empty String");
}
Similar things apply to the conditional AND operator (&&) and the
boolean AND operator (&).
<http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#54532>
<http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5242>
<http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5247>

Signature
Regards,
Roland de Ruiter
` ___ ___
`/__/ w_/ /__/
/ \ /_/ / \
Chris Uppal - 16 Jun 2005 10:38 GMT
> If 'if' is standardized and anyone has a link that
> explains that, that would be terrific for me to
> put in as a comment where I use such line.
Incidentally, this aspect of the Java definition is /extremely/ well-known (and
is common with C and C++ too), There would not normally be any point in
addinng a comment explaining it.
For instance, the following code is sufficiently self-explanatory that there
would be no point in explaining that slowCheck() is only used if quickCheck()
fails:
if (quickCheck() || slowCheck())
{
// ...whatever
}
-- chris
DeMarcus - 16 Jun 2005 17:48 GMT
Ok, thanks everyone for your answers.
Daniel
> Hi,
>
[quoted text clipped - 16 lines]
> Thanks
> Daniel Marcus