• 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

 
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From EnthuWare:
output is true,false,flase.Why?
Explanaion given:Java parses the expression from left to right. Once the it realizes that the left operand

of a conditional "or" operator has evaluated to true, it does not even try to evaluate the right side

expression.


But, && has higher precedence than || then howcome that it is not evaluated before ||.
 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Daisy please use code tags for code.



|| is a shortcut OR operator. It first checks the first part of the given expression. If it is true then rest of the expression is skipped. In case the first part of the expression is false then the remaining part of the expression is also checked.

Here in this line


when a=true is evaluated then it knows that the result is true. Don't forget that it assigns true to the boolean variable a while b and c retains their default values that is 'false'.

HTH
 
Bindu Lakhanpal
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Himanshu.I know || is Short circuit. And also that a=true is evaluated first of all.
But is it not true that expression evaluation respects the precedence order?
Like if
int f=3+4*5, then it is 3+(4*5).
There is something seriously wrong with my understanding or i am nervous cause my exam is near.
But my confusion is: && has higher precedence than || ,then why does || is getting evaluated first.
I hope i am conveying it properly.May be its because these are short circuit operator...
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes && has higher precedence as compared to ||. That is why the expression is evaluated in the following manner:


If you swap the && and || operators then the expression will be:


is same as


HTH
 
Bindu Lakhanpal
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok! I got it finally!Thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic