Java Programming - Operators and Assignments - Discussion

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");
    }
}

[A]. ok
[B]. dokey
[C]. ok dokey
[D]. No output is produced
[E]. Compilation error

Answer: Option B

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".


Bhagyashree said: (Jan 10, 2011)  
Please any one can explain it I have confusion.

Sinu Jos said: (Aug 23, 2011)  
Can anybody explain me?

Vinay said: (Dec 17, 2011)  
First if is false
and second one is true.
Hint:
Truth table concept is working here apply that logic you will get ans why first if is false and second one is true.

Govind said: (Jun 26, 2012)  
I can't understand please explain in the deeply.

Mayur Raiyani said: (Jul 3, 2012)  
Here is explanation:

First condition:

(b1 & b2 | b2 & b3 | b2).
(true & false | false & true | false).
(false | false | false).
(false).

Second Condition:

(b1 & b2 | b2 & b3 | b2 | b1).
(true & false | false & true | false | true).
(false | false | false | true).
(false | true).
(true).

Prikshit said: (Sep 26, 2014)  
Why is there no compilation error?

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

Quazi Faisal said: (Jun 17, 2015)  
Thanks for such a good explanation.

Quazi Faisal said: (Jun 17, 2015)  
Thanks for such a good explanation.

Aravinth said: (Aug 3, 2015)  
@Prikshit.

&& is used to define multiple conditions like if (i>100 && i<150).

But & is used for and operation. Same in case for || and | also.

Akash said: (Aug 16, 2015)  
Thanks for the clear and detailed explanation.

Ratan Pal said: (Jan 11, 2017)  
Great explanation @Mayur.

Neel said: (Jan 29, 2017)  
Nice explanation @Mayur.

Suresh said: (Mar 11, 2017)  
Nice explanation. Thank for explaining it.

Bansal said: (May 21, 2017)  
The difference between (&& or &) and (|| or |) is only that (&& and ||) performs a short-circuit whereas (& and |) does not otherwise operations of all && and &,(|| or | ) is same both as AND, OR operators.

Chander Kant said: (Feb 12, 2018)  
"if" only evaluates true condition. i.e. if(true) entry and if(false) get out.

Preethi said: (Aug 3, 2018)  
I can't understand the short-circuit logical operator and boolean logical operator difference, can anyone explain?

Sangeetha R said: (Sep 22, 2018)  
Logical AND (&&):

It evaluates to "true" if and only "if both operands of logical AND are true". If either or both operands are false, it evaluates to false.

Logical OR (||):
It evaluates to true if "either or both" of its operands are "true". If both operands are false, it evaluates to false.

Siva said: (Nov 13, 2019)  
"if" only evaluates true condition. i.e. if(true) entry and if(false) get out.

if ( b1 & b2 | b2 & b3 | b2 ){ /* Line 8 */ flase | flase | flase| ---> if(false) get out.
System.out.print("ok ");
}
if ( b1 & b2 | b2 & b3 | b2 | b1 ){ /*Line 10*/ flase | flase | flase| true---->"if" only evaluates true condition

System.out.println("dokey");

o/p:-- dokey

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.