Java Programming - Operators and Assignments - Discussion

Discussion Forum : Operators and Assignments - Finding the output (Q.No. 10)
10.
What will be the output of the program?
class SSBool 
{
    public static void main(String [] args) 
    {
        boolean b1 = true;
        boolean b2 = false;
        boolean b3 = true;
        if ( b1 & b2 | b2 & b3 | b2 ) /* Line 8 */
            System.out.print("ok ");
        if ( b1 & b2 | b2 & b3 | b2 | b1 ) /*Line 10*/
            System.out.println("dokey");
    }
}
ok
dokey
ok dokey
No output is produced
Compilation error
Answer: Option
Explanation:

The & operator has a higher precedence than the | operator so that on line 8 b1 and b2 are evaluated together as are b2 & b3. The final b1 in line 10 is what causes that if test to be true. Hence it prints "dokey".

Discussion:
21 comments Page 2 of 3.

Prikshit said:   1 decade ago
Why is there no compilation error?

Don't we need to use && and || instead of & and |.

Chander Kant said:   8 years ago
"if" only evaluates true condition. i.e. if(true) entry and if(false) get out.

Govind said:   1 decade ago
I can't understand please explain in the deeply.

Bhagyashree said:   1 decade ago
Please any one can explain it I have confusion.

Akash said:   1 decade ago
Thanks for the clear and detailed explanation.

Suresh said:   9 years ago
Nice explanation. Thank for explaining it.

Quazi Faisal said:   1 decade ago
Thanks for such a good explanation.

Quazi Faisal said:   1 decade ago
Thanks for such a good explanation.

Ratan Pal said:   9 years ago
Great explanation @Mayur.

Neel said:   9 years ago
Nice explanation @Mayur.


Post your comments here:

Your comments will be displayed after verification.