• 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

Precedence

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
My question is which of the following operators has highest precedence?
a). &&
b). ||

Which operator gets first evaluated when both are present in an expression?

in Khalid-Mughal book he has shown && operator having highest precedence than ||.

According to Khalid,the output of the following code should be "false,true,true" but i am getting "true,false,false"(jdk1.4.2_03).
can u tell me why so ?

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);
}}
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
true, false, false is correct.

Rewriting the expression with parentheses according to operator priority gives:

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

The first thing executed is the a = true assigment which also evaluates to true. Because || is a shortcut operator, the sub-expression ((b=true) && (c=true)) is not executed, therefore b and c remain at their defaulted values of false.

So a is true, b and c are false.
 
You don't know me, but I've been looking all over the world for. Thanks to the help from this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic