File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes conditional operator precedence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "conditional operator precedence" Watch "conditional operator precedence" New topic
Author

conditional operator precedence

Jeet Jain
Ranch Hand

Joined: Sep 01, 2011
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jeet Jain
Ranch Hand

Joined: Sep 01, 2011
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

Joined: Sep 20, 2010
Posts: 3647
    
  17

By the way, you are referring to logical operators, not conditional. Java has the ?: operator which is conditional.
Ananya Raval
Greenhorn

Joined: Aug 27, 2011
Posts: 5
I think it got evaluated in the following order:
b4=(b1||(!(b2&&b3)))
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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

Joined: Sep 01, 2011
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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

Joined: Sep 01, 2011
Posts: 53
thank you so much finally got it
Ilakya Mukunth
Ranch Hand

Joined: Mar 13, 2012
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: conditional operator precedence