Lesson+08


 * Lesson #08: Boolean Type and Operations - Ian

Boolean-** This data type can only have two values: True or False.

Create boolean variables as shown in the following two examples: code format="java" boolean b = true; boolean z = ((p < j) && (x != c)); code
 * 1. Creation of booleans:**

To compare quantities, such as (y = 97), we must do it this way: (Remember a single '=' is the assignment operator) code format="java" y == 97 code
 * 2. Comparing Quantities:**

In Java we don’t use the word “and” to indicate an AND operation or "or" to indicate an OR operation. We use “&&” for "AND" and "||" for "OR" instead. code format="java" AND operation --- ((x <10) && (y == 97))
 * 3. AND and OR operations:**

OR operation --- ((x <10) || (y == 97)) code Examples: code x = 3 and y = 97

((x <10) AND (y = 97)) Both parts are true so the whole thing is true. ((x <10) AND (y = -3)) First part is true, second part is false, whole thing false ((x <10) OR (y = 97)) If either part is true (both are) the whole thing is true. ((x <10) OR (y = -3)) If either part is true (first part is) the whole thing true. code

Another operator we need to know about is the not operator (!). It is officially called the negation operator. code 1. System.out.println(!true); //false 2. System.out.println(!false); //true 3. System.out.println( !(3 < 5) ); //false 4. System.out.println( !(1 == 0) ); //true code
 * 4. Negation operator:**

The operation of precedence is (from left to right): code !  ==   !=   &&   ||
 * 5. Operator precedence:**

code Example: code System.out.println( true || false && false); //true

Do the false && false part first to get a result of false. Now do true || false to get a final result of true. code