• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dan's Mock Exam Q - Operators and Precedence

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a question from Dan's mock test
class EBH202 {
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);
}}
What is the output?
The ans is: true,false,false
I understand the && and || are short-circuited operators, but isn't && has higher precedence over ||
Then how come variable a gets value true and not b and c?
My ans was: true,true,true
Reasoning:
&& has higher precedence, so (b=true) is evaluated first. b becomes true.
Since the first part is true, the second part (c=true) is evaluated as well. c becomes true.
Here i get a little confused - the second part of || is already evaluated! Anyway, i am assuming the first part of || also gets evaluated as well.
If you notice I am evaluating this expr the same way i would with an expr like x = a + b * c where the multiplication happens first and then the addition. Where did I go wrong?
Really appreciate your help.
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kumar,
yes you are correct with your assumption that '&&' has higher precedence over '||'. However you may also want to know that compilers don't see the expressions as we all humans do (at once). So you might want to change the way you look at the expression.
This is how the compiler looks at the expression
boolean x = (a = true) || ((b = true) && (c = true));
This expression is equivelant to the one you posted.
You may want to check out this code

Hope this helps.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Otherwise it will give you just "The local variable b may not have been initialized". The same stands for c.
Now it will return "true,false,false".
My conclusion is that operators || and && have the same precedence, the expression in question is evaluated from left to right and after evaluating a=true there's no need to evaluate ther right part of the || (short-circuit!), so b and c maintain the false value.
If we were to put

That's a whole different story, it would give back:
false,true,true

Cheers,
Bojan
[ December 26, 2003: Message edited by: Bojan Knezovic ]
 
S Kumar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys, for your response. I realised after I posted that question that operands are evaluated from left to right and this happens even before the expression is computed (except for ||, && and ?: operators which are special - the left hand operand is evaluated first and if needed the right hand operand). So, a= true, b=true and c=true are operands and are evaluated based on this rule.
As mentined before, only after the operands are evaluated the expression is computed and that is when precedence and associativity come into play.
Also, && has higher precedence that || (per Java in a Nutshell by David Flanagan and other books).
Take care.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic