Java Programming - Java.lang Class - Discussion

Discussion Forum : Java.lang Class - Finding the output (Q.No. 28)
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");
    }
}
z
x z
y z
Compilation fails.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
9 comments Page 1 of 1.

Vinay said:   1 decade ago
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:   1 decade ago
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:   1 decade ago
Here, & is non shortcut version and && is the shortcut version of the logical AND operator.

Ranju said:   1 decade ago
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:   1 decade ago
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:   1 decade ago
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:   1 decade ago
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:   9 years ago
Hello,

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

How it's happens? Please tell me.

SK MOINUDDIN ISLAM said:   8 years ago
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:

Your comments will be displayed after verification.