Which one is a valid declaration of a boolean?
-
boolean b1 = 0;
-
boolean b2 = 'false';
-
boolean b3 = false;
-
boolean b4 = Boolean.false();
A boolean can only be assigned the literal true or false.
In Java, boolean is a primitive type that only accepts the literal values true or false (not quoted, not numeric). 'boolean b3 = false;' is valid because false is the correct boolean literal. 'boolean b1 = 0;' fails because Java does not allow implicit conversion between int and boolean (unlike C/C++). 'boolean b2 = 'false';' fails because single quotes denote a char literal, and 'false' is a multi-character string, not a valid char, plus a char can't be assigned to boolean anyway. 'boolean b4 = Boolean.false();' is invalid because Boolean.false() isn't a method — false is a keyword/literal, not something invoked as a method call.