# Learn Java the Hard Way (Second Edition)

## Exercise 12: Boolean Expressions

So far we have only seen three types of variables:

`int`
integers, hold numbers (positive or negative) with no fractional parts
`double`
“double-precision floating-point” numbers (positive or negative) that could have a fractional part
`String`
a string of characters, hold words, phrases, symbols, sentences, whatever

But as a wise man once said, “There is another….” A “Boolean” variable (named after the mathematician George Boole) cannot hold numbers or words. It can only store one of two values: `true` or `false`. That’s it. We can use them to perform logic. To the code!

### What You Should See

On line 14 the Boolean variable a is set equal to something strange: the result of a comparison. The current value in the variable x is compared to the value of the variable y. If x’s value is less than y’s, then the comparison is true and the Boolean value `true` is stored into a. If x is not less than y, then the comparison is false and the Boolean value `false` is stored into a. (I think that is easier to understand than it is to write.)

Line 15 is similar, except that the comparison is “less than or equal to”, and the Boolean result is stored into b.

Line 16 is “equal to”: c will be set to the value `true` if x holds the same value as y. The comparison in line 17 is “not equal to”. Lines 18 and 19 are “greater than” and “greater than or equal to”, respectively.

On lines 21 through 26, we display the values of all those Boolean variables on the screen.

Line 29 through line 34 introduce the “not” operator, which is an exclamation point (`!`). It takes the logical opposite. So on line 29 we display the logical negation of “x is less than y?”, and we also print out the truth value of “x is greater than or equal to y?”, which are equivalent. (The opposite of “less than” is “greater than or equal to”.) Lines 30 through 34 show the opposites of the remaining relational operators.

(There is no video for this Study Drill yet.)

“Learn Java the Hard Way” is ©2013–2016 Graham Mitchell