Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operators Precedance: Output of the Program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Operators Precedance: Output of the Program" Watch "Operators Precedance: Output of the Program" New topic
Author

Operators Precedance: Output of the Program

Chandrakanth
Ranch Hand

Joined: Aug 16, 2005
Posts: 60
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);
}
}
rey anz
Greenhorn

Joined: Nov 10, 2005
Posts: 7
|| 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.
Anindo Ghosh
Greenhorn

Joined: Aug 11, 2005
Posts: 10
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.


Courage is resistance to fear, mastery of fear - not absence of fear - Mark Twain
Chaitanya Varanasi
Greenhorn

Joined: Dec 09, 2004
Posts: 26
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

Joined: Aug 16, 2005
Posts: 60
Thx for the reply,
Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Operators Precedance: Output of the Program
 
Similar Threads
Precedence on Short Circuit Operators
&& and || operator precedence
boolean x = (a = true) || (b = true) && (c = true);
Precedence
Output of this snippet.