Java Programming/Keywords/boolean
is a keyword which designates the primitive type. There are only two possible values: true and false. The default value for fields is false.
The following is a declaration of a private field named initialized, and its use in a method named synchronizeConnection.
Code section 1: Connection synchronization.
private boolean initialized = false;
public void synchronizeConnection() {
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 as int) to 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.