aspose file tools*
The moose likes Beginning Java and the fly likes boolean x = (a = true) || (b = true) && (c = true); Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "boolean x = (a = true) || (b = true) && (c = true);" Watch "boolean x = (a = true) || (b = true) && (c = true);" New topic
Author

boolean x = (a = true) || (b = true) && (c = true);

sashwatji gupta
Greenhorn

Joined: Jun 07, 2006
Posts: 3
Please help me with this:

class Test{
static boolean a, b, c;
public static void main (String[] args) {
boolean x = (a = true) || (b = true) && (c = true);
System.out.print(a + "," + b + "," + c);
}}

output : true,false,false

As I know the && operator has higher precedence than || ,if that is the case (b = true) && (c = true) should be evaluated first and the output should be different from the given one.

Can someone please explain the reason for the generated output.
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
really bad bad code.



see the difference?

jan
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
The higher precedence of && over || implies that parens could go like this:

boolean x = (a = true) || ((b = true) && (c = true));

In other words

boolean x = (a = true) || (blah_blah);

Now the left hand expression a=true evalates to true; Since || is defined to use conditional evaluation, its right-hand size is not evaluated. variable x is assigned true, a becomes true, and b and c where never assigned to, so they are left with their initial false values. QED.


There is no emoticon for what I am feeling!
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
Since the braces is used it takes higher precedence so only left side of (||) is executed and value of true is assigned to a and right side of (||) not executed and default value of b and c(false) is getting printed.
sashwatji gupta
Greenhorn

Joined: Jun 07, 2006
Posts: 3
Originally posted by Jan Groth:
really bad bad code.



see the difference?

jan



I know the difference b/w those two but I was just trying out things....the code is just fine ..i want explanation for the output generated...no changes in the code
sashwatji gupta
Greenhorn

Joined: Jun 07, 2006
Posts: 3
Originally posted by Jeff Albertson:
The higher precedence of && over || implies that parens could go like this:

boolean x = (a = true) || ((b = true) && (c = true));

In other words

boolean x = (a = true) || (blah_blah);

Now the left hand expression a=true evalates to true; Since || is defined to use conditional evaluation, its right-hand size is not evaluated. variable x is assigned true, a becomes true, and b and c where never assigned to, so they are left with their initial false values. QED.


If I am not wrong the innermost parens should be evaluated first and hence b=true should be executed....I am still confused
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Originally posted by sashwatji gupta:


If I am not wrong


But you are, that's the thing. The || operator does short-circuit evaluation, meaning it always evaluates its left argument first, and only then, if the left one is false, will it evaluate the right one. Operator precedence determines how the expressions are grouped, but once they're parsed, expressions are always evaluated from left to right. In other words,

a || b && c

means

a || (b && c)

due to operator precedence, but because the "||" is first, its arguments are evaluated first. The parse tree looks like



And it is evaluated top-down, left to right.


[Jess in Action][AskingGoodQuestions]
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
Originally posted by sashwatji gupta:


see the difference?

jan<hr></blockquote>


I know the difference b/w those two but I was just trying out things....the code is just fine ..i want explanation for the output generated...no changes in the code[/QB]



okay, i was not sure about that... but anyhow, in my eyes this is a side effect of logical evaluations, and should never ever ever be used in any code. maybe if you write a obfuscator, but there is no other benefit then this...

cheers,
jan
Periakaruppan Thiagarajan
Ranch Hand

Joined: Jul 26, 2005
Posts: 65
The logical operator && and || have equal precedence and the prder of evaluation depends on the position of logical operator. (Logical operator always evaluate from left to right)

For ex,
static boolean a,b,c;
x = (a=true) || (b=true) && (c=true)

Output is true, false, false

static boolean a,b,c;
x = (a=true) && (b=true) || (c=true)

Output is true, true, false
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Periakaruppan Thiagarajan:
The logical operator && and || have equal precedence


False!

Operator && has higher precedence than ||. Of these three expressions:

1. x || y && z
2. (x || y) && z
3. x || (y && z)

1 and 3 are equivalent.
Periakaruppan Thiagarajan
Ranch Hand

Joined: Jul 26, 2005
Posts: 65
Jeff,
I am not using any parenthesis around the logical operator. Please validate my example and give your comments.

Thanks,
Pk
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
My only comment is that && has higher precedence than ||.
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 989
    
    1
If I might....

It looks like some people aren't quite getting what the question is.

x = (a=true) || (b=true) && (c==true);

Since && has a higher precedence than ||, it seems like (b=true) && (c==true) should be evaulated first, thus setting b and c to true. If && has higher precedence, why is one of the operands for the || evaluated first?

And Mr. Friedman-Hill has explained it the best so far. All I might add is that precedence of operators doesn't dictate the order in which the operands are evaluated -- merely what operations must be completed as operands for for other operators if not rendered unnecessary by short-circuit operators. The precedence determines the tree for the expression (with, ironically, higher-precedent operators going lower in the tree), but then the tree is evaluated depth-first and left-to-right, regardless of operators precedence.



First the a=true is evaluated, with the "side effect" of doing the assignment, to a value of true. Since || short circuits, the other side isn't even looked at.

If you really want the && (and its operands) to be evaluated first, you'd have to rewrite the expression:

x = (b=true) && (c=true) || (a=true);

Of course, then b and c are set to true and a remains false because || short circuits.

I don't think I've explained anything new, just the same info in smaller steps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: boolean x = (a = true) || (b = true) && (c = true);
 
Similar Threads
about logical AND , OR
Precedence on Short Circuit Operators
Conditional Operators (try this)
Operators Precedance: Output of the Program
Output of this snippet.