Java Programming/Keywords/boolean
Tools
General
Sister projects
In other projects
boolean
is akeyword which designates theboolean
primitive type. There are only two possibleboolean
values:true
andfalse
. The default value forboolean
fields isfalse
.
The following is a declaration of aprivate
boolean
field namedinitialized
, and its use in a method namedsynchronizeConnection
.
![]() | Code section 1: Connection synchronization.privatebooleaninitialized=false;publicvoidsynchronizeConnection(){if(!initialized){connection=connect();initialized=true;}} |
The previous code only creates a connection once (at the first method call). Note that there is no automatic conversion between integer types (such asint
) toboolean
as is possible in some languages likeC. Instead, one must use an equivalent expression such as(i != 0)
which evaluates totrue
ifi
is not zero.