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.
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?
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).
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?
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...