Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# conditional operator precedence

Jeet Jain
Ranch Hand
Posts: 53
given:

boolean b1 = false, b2 = true, b3 = true;

does b4 = b1 || !b3 && b2;

get executed as b4 = (b1 || (!b3 && b2)); or b4 = ((b1 || !b3) && b2); ???

Henry Wong
author
Marshal
Posts: 21745
85
Jeet Jain wrote:
does b4 = b1 || !b3 && b2;

get executed as b4 = (b1 || (!b3 && b2)); or b4 = ((b1 || !b3) && b2); ???

Logical AND has higher precendence than logical OR, so the first choice.

Henry

Jeet Jain
Ranch Hand
Posts: 53
Thank you for your reply. but with the same initial values of b1,b2,b3 executing the following code:

boolean b4 = operandEval(1, b1) || !operandEval(2, b3) && operandEval(3, b2);
System.out.println();
System.out.println("Value of b4: " + b4);
static boolean operandEval(int opNum, boolean operand)
{
System.out.print(opNum);
return operand;
}

results in output:

12
Value of b4: false

???

Stephan van Hulst
Bartender
Posts: 6486
83
By the way, you are referring to logical operators, not conditional. Java has the ?: operator which is conditional.

Ananya Raval
Greenhorn
Posts: 5
I think it got evaluated in the following order:
b4=(b1||(!(b2&&b3)))

Henry Wong
author
Marshal
Posts: 21745
85
Jeet Jain wrote:
boolean b4 = operandEval(1, b1) || !operandEval(2, b3) && operandEval(3, b2);
System.out.println();
System.out.println("Value of b4: " + b4);
static boolean operandEval(int opNum, boolean operand)
{
System.out.print(opNum);
return operand;
}

results in output:

12
Value of b4: false

???

Let's not confuse precedence with the order of evaluation. As mentioned, precedence determines "where the parenthesis are", but unlike humans, the computer (and compiler) doesn't have to evaluate in the same order -- meaning it doesn't need to evaluate from the inner most parens outward. The order of evaluation, as defined by the Java specification, is for the most part, done from left to right. In this example, only the short circuiting rules directly affect the order of evaluation.

Henry

Jeet Jain
Ranch Hand
Posts: 53
Ohh thank you. But then that means that precedence(parentheses) has no relevance since it is going to be evaluated left to right anyways? And doesn't the NOT(!) operator have higher precedence than || and &&? so first the b2 operand should be evaluated, right?

Henry Wong
author
Marshal
Posts: 21745
85
Jeet Jain wrote:Ohh thank you. But then that means that precedence(parentheses) has no relevance since it is going to be evaluated left to right anyways? And doesn't the NOT(!) operator have higher precedence than || and &&? so first the b2 operand should be evaluated, right?

Well, I wouldn't say no relevance, as precedence affects what the logical (short-circuit) operators, which in turn, does affect the order of evaluation (to stop evaluating that is).

Henry

Jeet Jain
Ranch Hand
Posts: 53
thank you so much finally got it

Ilakya Mukunth
Ranch Hand
Posts: 57
Henry Wong wrote:
Jeet Jain wrote:Ohh thank you. But then that means that precedence(parentheses) has no relevance since it is going to be evaluated left to right anyways? And doesn't the NOT(!) operator have higher precedence than || and &&? so first the b2 operand should be evaluated, right?

Well, I wouldn't say no relevance, as precedence affects what the logical (short-circuit) operators, which in turn, does affect the order of evaluation (to stop evaluating that is).

Henry

The following,
boolean a,b,c;
a=b=c=false;
boolean x = (a = true) || (b = true) && (c = true);
System.out.println(a +" "+b +" "+c +" "+x);

output
true false false true

I read that the logical operator's precedence is && ^ and ||
I really do not understand when do I have to apply the precedence of && and ^
Can you help me?

Henry Wong
author
Marshal
Posts: 21745
85
Ilakya Mukunth wrote:
I read that the logical operator's precedence is && ^ and ||
I really do not understand when do I have to apply the precedence of && and ^

The logical AND has higher precedence than the logical OR, so this line...

is like this line...

Henry