File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operator Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Operator Problem" Watch "Operator Problem" New topic

Operator Problem

Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
What will be the output of the following program. Please tell me the reasons also.

class Lang21 {
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);
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
true, false, false

||,&& are short circuits operators, so after || it will stop the evaluation.

SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
Joseph Clark
Ranch Hand

Joined: Sep 10, 2005
Posts: 48
If you duplicate the line . . .

. . . in the first line in the method main(), then you will get a clear picture of what is going on. By default, the class variables are false. The code does something that makes one class variable true.
Sunil Kumar Gupta
Ranch Hand

Joined: Aug 26, 2005
Posts: 824
Your answer is true, false, false

<b>The || operator evaluates its right-hand operand only
if the value of its left-hand operand is false</b>

But here the value on the left side is a true value

the statement <b>a=true</b> is making it true on left side...

I think, it will help u ...

Lack of will power has caused more failure than lack of intelligence or ability.
My Blog | Red5 Resources | Technology Update | Daily Technology Tips
Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
Thanks guys! I got it.
I agree. Here's the link:
subject: Operator Problem
It's not a secret anymore!