Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

conditional operator precedence

 
Jeet Jain
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21194
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5893
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, you are referring to logical operators, not conditional. Java has the ?: operator which is conditional.
 
Ananya Raval
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it got evaluated in the following order:
b4=(b1||(!(b2&&b3)))
 
Henry Wong
author
Marshal
Pie
Posts: 21194
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21194
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much finally got it
 
Ilakya Mukunth
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21194
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic