Is 'if' standardized?

Discussion in 'Java' started by DeMarcus, Jun 16, 2005.

  1. DeMarcus

    DeMarcus Guest

    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
     
    DeMarcus, Jun 16, 2005
    #1
    1. Advertising

  2. 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


    DeMarcus wrote:

    >
    > 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, Jun 16, 2005
    #2
    1. Advertising

  3. DeMarcus

    Roland Guest

    On 16-6-2005 7:59, DeMarcus wrote:
    > 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_/ /__/
    / \ /_/ / \
     
    Roland, Jun 16, 2005
    #3
  4. DeMarcus

    Chris Uppal Guest

    DeMarcus wrote:

    > 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
     
    Chris Uppal, Jun 16, 2005
    #4
  5. DeMarcus

    DeMarcus Guest

    Ok, thanks everyone for your answers.

    Daniel


    DeMarcus wrote:
    >
    > 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
    >
    >
     
    DeMarcus, Jun 16, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Treseler

    standardized interfaces

    Mike Treseler, Nov 6, 2006, in forum: VHDL
    Replies:
    0
    Views:
    418
    Mike Treseler
    Nov 6, 2006
  2. Richo
    Replies:
    2
    Views:
    463
    Richo
    Dec 28, 2006
  3. Grant Robertson
    Replies:
    6
    Views:
    436
    Grant Robertson
    Jan 9, 2007
  4. Chris Mellon

    Re: standardized us address

    Chris Mellon, Feb 12, 2007, in forum: Python
    Replies:
    1
    Views:
    378
    John Nagle
    Feb 12, 2007
  5. alexru
    Replies:
    6
    Views:
    358
    Steve Holden
    Jan 9, 2010
Loading...

Share This Page