简体   繁体   中英

Does && operator really have greater precedence over || operator ? Look at the code

In Oracle Java Docs it is mentioned that && operator has higher precedence over || operator.

Please look at the following code:

class TestLogicalOperators
{
public static void main(String... args)
{

    if(doFalse() || doTrue1() && doTrue2() )
    {
        System.out.println(true+" inside if");
    }

}

static boolean doTrue1()
{
    System.out.println("doTrue1");
    return true;
}

static boolean doTrue2()
{
    System.out.println("doTrue2");
    return true;
}

static boolean doFalse()
{
    System.out.println("doFalse");
    return false;
}

}

The output is:
doFalse
doTrue1
doTrue2
true inside if

Now if && operator has higher precedence over || operator shouldn't the methods doTrue1() and doTrue2() be evaluated first before doFalse()?

No.

The && operator has precedence, insofar as the expression:

doFalse() || doTrue1() && doTrue2()

... can also be read as:

doFalse() || ( doTrue1() && doTrue2() ) doFalse() || ( doTrue1() && doTrue2() ) (note the parenthesis).

It doesn't mean the && expression will be evaluated before, your expression is still evaluated left to right.

The || operator can be a shortcut (ie no evaluation of second operand), if the first operand is true .

See example below:

// no shortcut, evaluates (true && false) and returns false anyway 
System.out.println(false || true && false);
// no shortcut, evaluates (true && true) and returns true
System.out.println(false || true && true);
// shortcut (see warning), evaluates true and disregards "&&" expression
System.out.println(true || false && false);

You are missunderstanding the documentation. It doesn´t mean that the && operator will get executed first, it just says that the conditions surrunding a && are precedence over an other condition with a lower precedence.

In your example you can notice that the compiler is checking the condition of the if statement from left to right.

If we would go straigt from left to right the condition would be: (note parenthesis)

if((doFalse() || doTrue1()) && doTrue2() ) 
{
  //This means either doFalse or doTrue1 would be true and doTrue2 would be true
}

But since the precedence of the && operator is higher then the on of the || operator it is read correctly as

if(doFalse() || (doTrue1() && doTrue2())) 
{
    //This correctly means either doFalse is true or doTrue1 and doTrue2 are true
}

NO, Normally first condition of OR operation is executed first. Which if evaluated to false, further checks second condition. Otherwise it doesn't.

Operator && has preference over || but evaluation from left to right is still present and has precedende over operators as described in JLS §15.7

The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM