© Doug Winnie 2021
D. WinnieEssential Java for AP CompScihttps://doi.org/10.1007/978-1-4842-6183-5_28

28. Boolean Values and Equality

Doug Winnie1  
(1)
Mission Hills, KS, USA
 

You have heard that everything with computers comes down to binary, or yes and no answers. That is true, and you can use that in your programs using Boolean values and logic operators. Boolean variables contain, and only contain, the values true or false. To determine a true or false response, there are a number of operators called logic operators that evaluate an equation as either true or false. You can then take that value and work with it or assign it to a Boolean variable.

Creating a Boolean Variable

Boolean variables can only contain two values: true or false. To create one, you initialize the variable using the Boolean value type like this:
Boolean a;
You can then assign a value of true or false to the variable:
a = true;

Setting a Boolean variable using a literal is just one way that you can assign a value, but it isn’t the most flexible way. Instead, the use of logic operators tests a number of scenarios to determine if a test, or condition, is true or false, and then the result of that test is assigned to the Boolean variable as true or false values.

Boolean Logic Operators

The main Boolean logic operators function on principles of mathematics. Basic operators compare two, and only two, values against each other. Values are then determined to be either equal, not equal, greater than, less than, or a combination of these to another value.
int a = 5;
Boolean result;
result = (a == 5); // Equality, result true
result = (a != 5); // Inequality, result false
result = (a > 5);  // Greater than, result is false
result = (a < 5);  // Less than, result is false
result = (a <= 5); // Greater than or equal to, result is true
result = (a >= 5); // Less than or equal to, result is true

Altering a Boolean Value

You can reverse the value of any Boolean evaluation or variable by using the Not operator, represented by an exclamation point added as a prefix to a value, variable or evaluation. Simply place it immediately before the evaluation or variable:
int a = 5;
Boolean result;
result = (a == 5);  // result true
result = !result;   // result false
result = !(a == 5); // result false

Combining Logic with Evaluations

In addition to literals and variables, you can use evaluations to determine Boolean values. Logic operators are always performed last in the order of operations (before the assignment operator), so you can create groupings of evaluations using parentheses to create more complex scenarios, including the use of values returned from methods.
int a = 5;
Boolean result;
result = (5 + 5) > 10; // false

Compound Logic Operators

To create more complex evaluations, you can use the logical “AND” and “OR” operators. For OR, any one of the values needs to be true for the entire evaluation to be true. For AND, all values need to be true for the entire evaluation to be true.
int a = 5;
Boolean result;
result = (a < 5) || (a / 2 < 5); // OR: true
result = (a < 5) && (a / 2 < 5); // AND: false

Code Examples

The next page shows examples of capturing input from the user.

This code is also available in the following GitHub repo:

https://github.com/Apress/essential-java-AP-CompSci
public class Main {
    public static void main(String[] args) {
        int a = 5;
        Boolean result;
        System.out.println("Basic operators");
        result = (a == 5); // Equality, result true
        display(result);
        result = (a != 5); // Inequality, result false
        display(result);
        result = (a > 5);  // Greater than, result is false
        display(result);
        result = (a < 5);  // Less than, result is false
        display(result);
        result = (a >= 5); // Greater than or equal to, result is true
        display(result);
        result = (a <= 5); // Less than or equal to, result is true
        display(result);
        System.out.println("Not operator");
        result = !result;  // Not operator, true becomes false
        display(result);
        result = !true;    // true becomes false
        display(result);
        result = !(a < 5); // false becomes true
        display(result);
        System.out.println("Evaluations and returned values");
        result = (5 + 5) > 10; // false
        display(result);
        result = getNumber() < .5; // true or false, depends on the value returned
        display(result);
        System.out.println("OR");
        result = true || true;   // true
        display(result);
        result = true || false;  // true
        display(result);
        result = false || true;  // true
        display(result);
        result = false || false; // false
        display(result);
        result = (a < 5) || (a / 2 < 5); // true
        display(result);
        System.out.println("AND");
        result = true && true;   // true
        display(result);
        result = true && false;  // false
        display(result);
        result = false && true;  // false
        display(result);
        result = false && false; // false
        display(result);
        result = (a < 5) && (a / 2 < 5); // false
        display(result);
    }
    public static void display(boolean result) {
        System.out.println("The result is " + result);
    }
    public static void display(double result) {
        System.out.println("The value is " + result);
    }
    public static double getNumber() {
        double rand = Math.random();
        display(rand);
        return rand;
    }
}
Listing 28-1

Example Boolean logic code

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.144.32