• 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

Dans' Question Doubt

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code:
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);
}}

Answer true , false,false . I wonder why?
My assumption is && is of higher precedence than ||. So the answer should be true,true,true.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i guess || and && have the same proirity. so it evaluate from left to right. so in the code, after the ||, b and c are not evaluated.
 
stable boy
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The conditional operator "&&" is not evaluated because variable a is assigned the value of true, the remaining of the expression doesn't matter. The outcome will always be true.
Ofcourse this leaves the variables b and c assigned to their default value. i.e. false, false
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Can anyone please confirm that || and && have the same precedence since i read in Khalid Mughal's book that && has higher precedence than ||.
If && has higher precendence then true,false , false cannot be the answer

Also can anyone post something like a precedence table for the most commonly used operators??

Thanks,
Shiva
 
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
&& does have a higher precedence than ||, but it does not matter. The conditional operators &&, ||, and ?: are not evaluated in order of precedence , they are evaluated left to right. The only reason the conditional operators are even assigned a precedence is so they may be evaulated before any assignment or combinated assignment operator (which are the only types of operators with a lower precedence).

The following is a quote from the java language specification:

(When using ||) At run time, the left-hand operand expression is evaluated first; if its value is true, the value of the conditional-or expression is true and the right-hand operand expression is not evaluated. If the value of the left-hand operand is false, then the right-hand expression is evaluated and its value becomes the value of the conditional-or expression.

 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can find an order of precedence table in the java tutorial.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, precedence is enforced between && and ||. Left-to-right associativity applies to multiple && or multiple ||, not to a mixture of them.
Try the following:
Answer:
true
false
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic