Java Programming - Java.lang Class - Discussion

28. 

What will be the output of the program (in jdk1.6 or above)?

public class BoolTest 
{
    public static void main(String [] args) 
    {
        Boolean b1 = new Boolean("false");
        boolean b2;
        b2 = b1.booleanValue();
        if (!b2) 
        {
            b2 = true;
            System.out.print("x ");
        }
        if (b1 & b2) /* Line 13 */
        {
            System.out.print("y ");
        }
        System.out.println("z");
    }
}

[A]. z
[B]. x z
[C]. y z
[D]. Compilation fails.

Answer: Option B

Explanation:

No answer description available for this question.

Vinay said: (Mar 9, 2011)  
Hi, we can assign the Boolean wrapper object to boolean primitive values, eg: boolean bool = Boolean.FALSE is acceptable java code. Thus it takes the Boolean wrapper objects value and it prints "xz", so the answer is Option B

Tangent said: (Jul 19, 2011)  
Well, I got this question wrong...

I think this only works in JDK 1.5+

Prior to that, you would need to unbox the b1 wrapper object in order to do the bitwise & on line 13.

I hope my explanation can make some other old-timers, like myself, feel better about themselves :-)

Cheers,
Tom.

Sohan said: (Oct 19, 2012)  
Here, & is non shortcut version and && is the shortcut version of the logical AND operator.

Ranju said: (Jan 22, 2014)  
Boolean b1 is initialized with false & Boolean b2 is initialized with false due to line no 7(Boolean b2 = b1.BooleanValue();).

Now the if loop will return true as !FALSE is TRUE & thus will print x.

if( b1 & b2) => false & true =>false.

The next loop will return false as the value of b2 is changed from FALSE to TRUE because of the statement b2=true on line 10.

Thus b1=false and b2=true.

Thus the output is xz.

Rohit said: (Mar 15, 2014)  
x z.

Answer:The compiler fails at line 13 because b1 is a reference variable to a Boolean wrapper object, not a boolean primitive. Logical boolean tests can't be made on Boolean objects.

Kaiwalya said: (Dec 2, 2014)  
boolean b=new boolean("false");

I think this is wrong. Because value is passed as "false" in constructor. Which is treated as String. Can we pass string parameter in constructor of boolean class...?

Saisanthosh said: (Feb 9, 2015)  
Hello all,

I just tried this Boolean b1=false;in place of declaring 'b'. We can use boolean b; as well. But the problem is boolean is primitive data type which don't support booleanValue() method.

That is why they are using Boolean wrapper class rather than boolean primitive data type.

Please correct me in case of any thing wrong in my guess. Thanks!

Ankit said: (Nov 19, 2016)  
Hello,

& operator is work on bit.
&& operator is work on boolean value.

How it's happens? Please tell me.

Sk Moinuddin Islam said: (Apr 6, 2017)  
Here "false" is just a string, so boolean b1,b2 default value is False. So, first loop !false = true, than b2= true,

2nd loop b1=false & b2=true== false,
After that last sop z read, so output is x,z.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.