Java — Which one is a valid boolean variable declaration and initialization?

Difficulty: Easy

Correct Answer: boolean b3 = false;

Explanation:


Introduction / Context:
Java requires booleans to be assigned the literals true or false. There is no implicit numeric or string conversion to boolean.



Given Data / Assumptions:

  • The target type is primitive boolean.
  • We are evaluating literal and method-based initializations.


Concept / Approach:
Only boolean literals true/false or boolean expressions can initialize a boolean. Numeric 0/1, string literals, or non-existent helpers like Boolean.false() are invalid in Java.



Step-by-Step Solution:

Option A: Invalid — 0 is an int, not a boolean, and no implicit conversion exists.Option B: Invalid — 'false' is a char literal; not a boolean.Option C: Valid — uses the boolean literal false.Option D: Invalid — there is no Boolean.false() factory method.Option E: Invalid — “no” is not a recognized boolean literal.


Verification / Alternative check:
Compile each declaration; only Option C succeeds.



Why Other Options Are Wrong:
They use incompatible types or non-existent APIs.



Common Pitfalls:
Transferring habits from C/C++ (0/1 to represent booleans) to Java, where it is not allowed.



Final Answer:
boolean b3 = false;

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion