The boolean
type represents a logical quantity with two possible values, indicated by the literals true
and false
(§3.10.3). The boolean operators are:
==
and !=
(§15.20.2)
!
(§15.14.6)
&
, ^
, and |
(§15.21.2)
&&
(§15.22) and ||
(§15.23)
? :
(§15.24)
+
(§15.17.1), which, when given a String
operand and a boolean operand, will convert the boolean operand to a String
(either "true"
or "false"
), and then produce a newly created String
that is the concatenation of the two strings
Boolean expressions determine the control flow in several kinds of statements:
if
statement (§14.8)
while
statement (§14.10)
do
statement (§14.11)
for
statement (§14.12)
A boolean
expression also determines which subexpression is evaluated in the
conditional ? :
operator (§15.24).
Only boolean
expressions can be used in control flow statements and as the first operand of the conditional operator ? :
. An integer x
can be converted to a boolean
, following the C language convention that any nonzero value is true
, by the expression x!=0
. An object reference obj
can be converted to a boolean
, following the C language convention that any reference other than null
is true
, by the expression obj!=null
.
A cast of a boolean
value to type boolean
is allowed (§5.1.1); no other casts on type boolean
are allowed. A boolean
can be converted to a string by string conversion (§5.4).