• 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

Operator Precedence

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider this statements:
boolean passingScore = false && grade == 70;

The expression grade == 70 not get evaluated due to the false value of operand left to &&
but according to operator precedence, '==' should be evaluated first then why not grade == 70 get evaluated first? why && is given first preferance here?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are right == has higher precedence over &&.
BUT this is only in the case the expression (grade==70) gets evaluated which is not the case here since false short-circuit the whole boolean expression (false && grade == 70)
Higher precedence means that the expression gets evaluated like follows :
false && (grade == 70)
and not
(false && grade) == 70 // this would be invalid though !!
HIH

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are prefectly correct.
== precedes &&
In your case its happening too
but you dont see it since false is what comesout eventually coz its AND
int grade = 170;
boolean b = true && grade==70; //false
Ragu
 
reply
    Bookmark Topic Watch Topic
  • New Topic