< Java Programming < Keywords
boolean
is a keyword which designates the boolean
primitive type. There are only two possible boolean
values: true
and false
. The default value for boolean
fields is false
.
The following is a declaration of a private
boolean
field named initialized
, and its use in a method named synchronizeConnection
.
Code section 1: Connection synchronization.
1 private boolean initialized = false;
2
3 public void synchronizeConnection() {
4 if (!initialized) {
5 connection = connect();
6 initialized = true;
7 }
8 }
|
The previous code only creates a connection once (at the first method call). Note that there is no automatic conversion between integer types (such as int
) to boolean
as is possible in some languages like C. Instead, one must use an equivalent expression such as (i != 0)
which evaluates to true
if i
is not zero.
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.