Java Data and Operators

2. Boolean Data and Operators

George Boole

As we have learned, the boolean type is one of Java's primitive types. For this type, there are only two possible values, true and false. The boolean type is derived from the work of British mathematician George Boole, who in the 1850s, developed an algebra to process logical expressions such as p and q. Such boolean expressions produce a value that is either true or false. Every modern programming language provides some means of representing boolean expressions.


Conditional statement

The boolean type has several important uses. As we saw in Chapter 1, expressions of the form num == 7 and 5<7 have boolean values. Similarly, as we saw in Chapter 3, the boolean type is also used to represent the condition in the if statement:

if (boolean expression)
statement ;


Boolean flag

For this reason, boolean expressions are also called conditions. Along these same lines, a boolean variable can be used as a flag or a signal to "remember" whether or not a certain condition holds. For example, in the following code fragment, we use isDone to mark when a particular process is completed:

boolean isDone = false;        // Initialize the flag
...                            // Do some processing task
isDone = true;                 // Set flag when the task done
...                            // Do some other stuff
if (isDone)                    // Check if finished the task
...                            // If so, do something
else                                      
...                           // Or, do something else