Difficulty: Medium
Correct Answer: 10010
Explanation:
Introduction / Context:
This program checks subtle differences between reference equality (==) and value equality (equals) for Boolean wrapper objects created from String inputs, and it demonstrates that the String constructor for Boolean is case-insensitive for the literal "true".
Given Data / Assumptions:
Concept / Approach:
The Boolean(String) constructor yields true if the string (ignoring case) equals "true"; otherwise false. The operator == compares object references, not contained values, while equals compares the primitive boolean value inside the wrappers.
Step-by-Step Solution:
b1 == b2 → false (distinct objects) → result stays 0. b1.equals(b2) → true (true equals true) → result = 0 + 10 = 10. b2 == b4 → false → no change. b2.equals(b4) → false (true vs false) → no change. b2.equals(b3) → true (true vs true) → result = 10 + 10000 = 10010. Printed string is "result = 10010".
Verification / Alternative check:
Switch to Boolean.valueOf to see interning effects for certain paths; == may still not be reliable across distinct wrapper instances—always prefer equals for value comparison.
Why Other Options Are Wrong:
Values 0/1/10 ignore one or more equals checks; any other sum miscounts the lines that evaluate to true.
Common Pitfalls:
Assuming == compares content for wrappers; forgetting constructor case-insensitivity for "true".
Final Answer:
10010
Discussion & Comments