Java Programming - Java.lang Class - Discussion

Discussion Forum : Java.lang Class - Finding the output (Q.No. 3)
3.
What will be the output of the program?
public class BoolTest 
{
    public static void main(String [] args) 
    {
        int result = 0;

        Boolean b1 = new Boolean("TRUE");
        Boolean b2 = new Boolean("true");
        Boolean b3 = new Boolean("tRuE");
        Boolean b4 = new Boolean("false");

        if (b1 == b2)  /* Line 10 */
            result = 1;
        if (b1.equals(b2) ) /* Line 12 */
            result = result + 10;
        if (b2 == b4)  /* Line 14 */
            result = result + 100;
        if (b2.equals(b4) ) /* Line 16 */
            result = result + 1000;
        if (b2.equals(b3) ) /* Line 18 */
            result = result + 10000;

        System.out.println("result = " + result);
    }
}
0
1
10
10010
Answer: Option
Explanation:

Line 10 fails because b1 and b2 are two different objects. Lines 12 and 18 succeed because the Boolean String constructors are case insensitive. Lines 14 and 16 fail because true is not equal to false.

Discussion:
9 comments Page 1 of 1.

Naveen said:   6 years ago
b1==b2 - compare only the memory address of the string present.
b1.equals(b2) - compare the string which presents in the particular memory address.
(1)

Michael said:   8 years ago
What is the difference between b1 == b2 and b1 equals (b2)?

Swati jadhav said:   9 years ago
I didn't get the correct answer. Can anyone show me memory representation of string in this example?

Yash said:   1 decade ago
Can not understand properly will anyone please explain?

Krishna said:   1 decade ago
Yes the Boolean string constructor are insensitive so result 10010.

Pradeep said:   1 decade ago
As b1 and b2 are two different objects hence condition (b1 == b2) will fail where as condition on line 12 (b1.equals(b2)) will succeed.

Only line 12 & 18 will succeed.

Line 12 : result = 0 + 10 = 10.
Line 18: result = 10 + 10000 = 10010.

Saroja said:   1 decade ago
Yes the Boolean String constructors are insensitive. But how the value of result would be 10010. Would you please explain briefly?

Karthik said:   1 decade ago
How came this output?

Ramu said:   1 decade ago
is this(case insensitive) only for Boolean

Post your comments here:

Your comments will be displayed after verification.