Variables that can only have one of two values, true or false, are of type boolean, and the values true and false are boolean literals. You can define a boolean variable, called state, with the statement:
boolean state = true;
This statement also initializes the variable state with the value true. You can also set a boolean variable in an assignment statement. For example, the statement:
state = false;
sets the value of the variable state to false.
At this point we can't do much with a boolean variable, other than to set its value to true or false, but, as you will see in the next chapter, Boolean variables become much more useful in the context of decision making in a program, particularly when we can use expressions that produce a boolean result.
There are several operators that combine Boolean values including operators for Boolean AND, Boolean OR, and Boolean negation (these are &&, ||, and !, respectively), as well as comparison operators that produce a Boolean result. Rather than go into these here in the abstract, we will defer discussion until the next chapter where we will also look at how we can apply them in practice to alter the sequence of execution in a program.
One point you should note is that boolean variables differ from the other primitive data types in that they cannot be cast to any other basic type, and the other primitive types cannot be cast to boolean.