Is 'if' standardized?

D

DeMarcus

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
 
J

Jacques-Olivier Haenni

R

Roland

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

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>
--
Regards,

Roland de Ruiter
` ___ ___
`/__/ w_/ /__/
/ \ /_/ / \
 
C

Chris Uppal

DeMarcus said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top