Why will the following code not compile? Boolean b = new Boolean("false"); if (b) {}
-
- The quotes should not be around false.
-
- New should be after Boolean.
-
- A boolean is expected not a Boolean.
-
- The Semi-colon should not be used.
C
Correct answer
Explanation
In Java, you cannot use a Boolean wrapper object directly in an if statement - the condition expects a primitive boolean. The Boolean object must be unboxed (either explicitly via booleanValue() or through auto-unboxing in some contexts). The code fails because 'if (b)' attempts to use the Boolean object as a boolean primitive.