So far we have looked at three fundamental variable types… int, double, and String. In this lesson we will look at another very important type of variable, “boolean”. This type of variable has only two possible values, true or false.
Consider the following example. Suppose that we know that x = 3 and y = 97. Below are statements about x and y, and whether or not they are true or false individually, AND whether the entire statement is true or false.
Statement | True or False |
---|---|
(( x < 10 ) AND ( y = 97 ) |
First part is true, second part is true, entire statement is true |
(( x < 10 ) AND ( y = -3 ) |
First part is true, second part is false, entire statement is false |
(( x < 10 ) AND ( y ≠-3 ) |
First part is true, second part is true, entire statement is true |
(( x < 10 ) OR ( y = 97 )) |
Either part is true, entire statement is true |
(( x < 10 ) OR ( y = -3 )) |
Either part is true, entire statement is true |
The above examples illustrate how true and false statements are evaluated, however the syntax is not correct. In order for java to correctly read the syntax the “AND” and “OR” statements must be replaced with the correct symbols as shown below,
Statement | Java syntax explanation |
---|---|
(( x < 10 ) && ( y == 97 ) |
And is replaced with &&, = is replaced with “==” |
(( x < 10 ) && ( y == -3 ) |
And is replaced with &&, = is replaced with “==” |
(( x < 10 ) && ( y != -3 ) |
And is replaced with &&, ≠ is replaced with “!=” |
(( x < 10 ) || ( y == 97 )) |
OR is replaced with ||, = is replaced with "==" |
(( x < 10 ) || ( y == -3 )) |
OR is replaced with ||, = is replaced with "==" |
Truth tables are helpful for showing how && and || work for various combinations. These are illustrated below,
a | b | (a && b) | a | b | (a || b) | |
---|---|---|---|---|---|---|
false | false | false | false | false | false | |
false | true | false | false | true | true | |
true | false | false | true | false | true | |
true | true | true | true | true | true |
The not (or negation) operator is used to indicate opposite. For example, what is the opposite of true?... False of course. The “not” operator is indicated with the ! sign. Below are some examples.
System.out.println(!true);//false
System.out.println(!false);//true
System.out.println(!(3<5));//false
System.out.println(!(1==0));//true
Boolean variables are denoted with the word “boolean” and can be assigned a value that evaluates to true or false. Below are three examples,
boolean a;//declares a boolean variable a
boolean b = true;//declares and initializes boolean variable b
boolean z = (( p < j ) && ( x != c)); //declares and initializes boolean z
Just like math operations follow an order of precedence, so too do operations like AND (&&) and OR (||). Consider a problem like,
System.out.println( ( true && false ) || ((true && false) || false) );
In this example we can tell what parts we should do first because of the grouping by parenthesis. However, what if we had a different problem like the one below?
System.out.println( false && true || true );
Which part do we do first? As it turns out we would first do && and then ||. Because false and true are not the same thing, false && true evaluates to false. Next we consider false or true, which is true. The order of precedence for the operators we are studying are as follows,
!     ==     !=     &&     ||
The order of precedence for operators is illustrated in the following examples,
example | explanation |
---|---|
System.out.println(true || false && false); |
Do the false && false part first to get a result of false Now do true||false to get a final result of true |
System.out.println(true && false || false); |
Do the true && false part first to get a result of false. Now do false || false to get a final result of false |
DeMorgan’s Theorems are basically two sets of rules or laws developed from the Boolean expressions for AND, OR and NOT using two input variables, A and B. These two rules or theorems allow the input variables to be negated and converted from one form of a Boolean function into an opposite form.
DeMorgan’s first theorem states that two (or more) variables NOR'ed together is the same as the two variables inverted (Complement) and AND´ed, while the second theorem states that two (or more) variables NAND'ed together is the same as the two terms inverted (Complement) and OR´ed. That is replace all the OR operators with AND operators, or all the AND operators with an OR operators.
The video below explains this more clearly.