| Author |
Dan's exam
|
Damien Howard
Ranch Hand
Joined: Apr 01, 2003
Posts: 456
|
|
Question 17 class A { static boolean a; static boolean b; static boolean c; public static void main ( String[] args) { boolean x = (a = true) || (b = true) && (c = true); System.out.print(a + "," + b + "," + c); } } What is the result of attempting to compile and run the above program? a. Prints: false,false,false b. Prints: false,false,true c. Prints: false,true,false d. Prints: false,true,true e. Prints: true,false,false f. Prints: true,false,true g. Prints: true,true,false h. Prints: true,true,true i. Runtime error j. Compiler error k. None of the above
I thought the answer was h because && has higher precedence than || according to the table on this webpage http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html But Dan's solutions say the answer is e. Is this a mistake? If not can someone please explain it to me? Thanks
|
 |
Andres Gonzalez
Ranch Hand
Joined: Nov 27, 2001
Posts: 1561
|
|
Two things: 1- it's like saying if (a=true) OR( (b = true) AND (c = true) ) 2-Short circuit operator (||). You can forget about the right side, after the OR operator, since the first is true, not evaluating the rest. Hope it helps [ July 10, 2003: Message edited by: Andres Gonzalez ]
|
I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
|
 |
Damien Howard
Ranch Hand
Joined: Apr 01, 2003
Posts: 456
|
|
1- it's like saying if (a=true) OR( (b = true) AND (c = true) )
Why can you say this? The way I see it && has precedence over || and thus should be evaluated before ||. I understand that the way your wrote it would only evaluate a=true because of the short circuit, but since it was not written this way in the question why would you decide to group the expressions as you wrote it?
|
 |
Andres Gonzalez
Ranch Hand
Joined: Nov 27, 2001
Posts: 1561
|
|
Please refer to page 216 of KnB book: if ((x>3) && (y<2) | doStuff())
if (x>3)and either (y<2) or the result of doStuff() is true, then....
does that make sense?
|
 |
Andres Gonzalez
Ranch Hand
Joined: Nov 27, 2001
Posts: 1561
|
|
Forgot to mention. evaluations are from left to right as well. cheers [ July 10, 2003: Message edited by: Andres Gonzalez ]
|
 |
Alton Hernandez
Ranch Hand
Joined: May 30, 2003
Posts: 443
|
|
Originally posted by Damien Howard: The way I see it && has precedence over || and thus should be evaluated before ||.
Hi Damien, I think thats the very reason why the expression has to be grouped that way. This expression boolean b = a || c && d; is equivalent to this: boolean b = a || (c && d); Much like this expression int a = c + d*e; is equivalent to this int a = c + (d*e); Hope that helps.
|
 |
Damien Howard
Ranch Hand
Joined: Apr 01, 2003
Posts: 456
|
|
This expression boolean b = a || c && d; is equivalent to this: boolean b = a || (c && d); Much like this expression int a = c + d*e; is equivalent to this int a = c + (d*e); Hope that helps.
Exactly and in the above case you evaluate * first because it has precedence, so following the same logic b&&c should be evaluated first in my opinion. As to the example Andres referred to in KNB's book pg 216, that is a different example, because | has precedence over && or ||. precedence order is &, ^, |, &&, ||
|
 |
Alton Hernandez
Ranch Hand
Joined: May 30, 2003
Posts: 443
|
|
Hi Damien, Follow this link and go to Marlene's post. She got a very good explanation of what operator precedence means.
|
 |
Anupam Sinha
Ranch Hand
Joined: Apr 13, 2003
Posts: 1088
|
|
Hi Damien I am not sure about my answer but just trying. I think whats happening is that a which is true is evaluated first becasue b and c are || operands. So you can consider this as a || (b && c). Now the term (b && c) is also acting as the second operand for the ||. Now if a is false then the second operand of || would be evaluated but that is not the case. So (b && c) is not being evaluated. So it appears that the precedence is not being followed. [ July 10, 2003: Message edited by: Anupam Sinha ]
|
 |
Andres Gonzalez
Ranch Hand
Joined: Nov 27, 2001
Posts: 1561
|
|
Operator precedence is about how to add extra parentheses, *not* about the order of evaluation.
excellent link...
|
 |
Damien Howard
Ranch Hand
Joined: Apr 01, 2003
Posts: 456
|
|
|
Thanks, Alton. That link was helpful
|
 |
Alex Radomski
Greenhorn
Joined: Apr 24, 2003
Posts: 24
|
|
Thanks, poor (Antonym) link. btw Antonym isn't cast.. ha
|
 |
 |
|
|
subject: Dan's exam
|
|
|