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!

 
 1 import java.util.Scanner;
 2 
 3 public class BooleanExpressions {
 4     public static void main( String[] args ) {
 5         Scanner keyboard = new Scanner(System.in);
 6         boolean a, b, c, d, e, f;
 7         double x, y;
 8 
 9         System.out.print( "Give me two numbers. First: " );
10         x = keyboard.nextDouble();
11         System.out.print( "Second: " );
12         y = keyboard.nextDouble();
13 
14         a = (x <  y);
15         b = (x <= y);
16         c = (x == y);
17         d = (x != y);
18         e = (x >  y);
19         f = (x >= y);
20 
21         System.out.println( x + " is LESS THAN " + y + ": " + a );
22         System.out.println( x + " is LESS THAN / EQUAL TO " + y + ": " + b );
23         System.out.println( x + " is EQUAL TO " + y + ": " + c );
24         System.out.println( x + " is NOT EQUAL TO " + y + ": " + d );
25         System.out.println( x + " is GREATER THAN " + y + ": " + e );
26         System.out.println( x + " is GREATER THAN / EQUAL TO " + y + ": " + f );
27         System.out.println();
28 
29         System.out.println( !(x < y) + " " + (x >= y) );
30         System.out.println( !(x <= y) + " " + (x > y) );
31         System.out.println( !(x == y) + " " + (x != y) );
32         System.out.println( !(x != y) + " " + (x == y) );
33         System.out.println( !(x > y) + " " + (x <= y) );
34         System.out.println( !(x >= y) + " " + (x < y) );
35     }
36 }

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