• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Operators Precedance: Output of the Program

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you pls, tell me how is the output of this prog is true false false.

my understanding is this:
() have higher precedance than || and && so all of the () operators should be evaluated?
so abc will get the value of true.
than only short circuit operators should be applied...

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);
}
}
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
|| and && are shortcircuit operaters hence after evaluating (a=true) no (..) are evaluated and as a, b and c had been initialized to false, so the output is true false false which are the values for a, b and c respectively.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
boolean x = (a = true) || (b = true) && (c = true);

Since && has a higher precedence than the || operator,
the expression evaluates to

boolean x = (a = true) || ( (b = true) && (c = true) );

Now the *short-circuit* operators come into play.
As soon as (a=true) evaluates to true, the runtime abandons the evaluation of the rest of the expression.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The output which it shows is absolutely right, because if you have seen the condition , the conditional Operator is short-circuited OR operator.
So While evaluating the Expression, the Short-circuted OR will look if the first operand is true or not.. If its true, then it doesnt try to execute the other part of the expression, which is the reason why the values of A,B,C will retain the default values.

Hope you have got the answer,
Cheers
 
Chandrakanth
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx for the reply,
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
boolean x = (a = true) || (b = true) && (c = true);

here && operator is having highest precedence, so the expression (b=true) should be evaluated first, since it is short circuted operator (c-true) will be ignored.
then the expression will be
boolean x = (a=true) || true //c=true is not evauated hence c is false

can u pls tell me why b is false in the output
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic