Boolean Expressions

public class EqualityRelationalCondition {
    static int x = 5;

    public static void greaterStatement(){
        if (x <= 2){
            System.out.println("ABC"); //doesn't print abc because 5 is not less or equal to 2
        }
    }

    public static void andStatement() {
        if (x >= 8 && 5/0 > 3){
            System.out.println("DEF"); //doesn't print def because 5 is not greater than 8
        }

        /* if (x >= 8 && 5/0 > 3){
            System.out.println("DEF"); //doesn't print because error of dividing by 0. Since "x >=8" is true, 
        } */
    }

    public static void orStatement() {
        if (x > 3 || x == 10){ //since first is true, it won't check the second value
            System.out.println("GHI");
        }

        if (x < 3 || x == 5){ //since first is true, it won't check the second value
            System.out.println("JKL");
        }
    }
    
    public static void main(String[] args){
        greaterStatement();
        andStatement();
        orStatement();
    }
}

EqualityRelationalCondition.main(null);
GHI
JKL

if, else if, and else:

  • if and else if require a boolean expression while else doesn’t
  • an else if statement must have another else if to “latch on to”
  • an else must have a if or else if to latch on to
public class Statements {
    public static void ifStatementOnly(){
        int x = 5;
        if (x < 10){
            System.out.println("A");
        }
        if (x < 4){
            System.out.println("B");
        }
        if(x < 7){
            System.out.println("C");
        }
    }

    public static void ifElseStatementOnly(){
        int y = 5;
        if (y > 10){
            System.out.println("D");
        }
        else{
            System.out.println("E");
        }
    }

    public static void elseIfStatements(){
        int z = 5;
        if (z > 10){
            System.out.println("F");
        }
        else if (z >= 4){
            System.out.println("G");
        }
        else if (z < 1){
            System.out.println("H");
        }
        else {
            System.out.println("I");
        }
    }

    public static void main(String[] args){
        ifStatementOnly();
        ifElseStatementOnly();
        elseIfStatements();
    }
}

Statements.main(null);
A
C
E
G

Nested if, else if, and else

  • An if, else if, and else can be “nested” inside a block of code attached to another if, else if, and else
  • Other control strucutres like while, do while, for, etc. can be nested
public class Nested {
    public static void nestedIfElseStatements() {
        int x = 5;
        int y = 3;

        if (x < 10){
            if (y != 3){ //if statement nested in a if statement
                System.out.println("A");
            }
            else if (y <= 5) {//else if statement nested in a if statement
                System.out.println("B");
            }
            else {//else statement nested in a if statement
                System.out.println("C");
            }
        }
        else {
            if (y != 3){//if statement nested in a else statement
                System.out.println("D");
            }
            else if (y <= 5){//else if statement nested in a else statement
                System.out.println("E");
            }
        }
    }

    public static void main(String[] args){
        nestedIfElseStatements();
    }
}

Nested.main(null);
B

De Morgan’s Laws

  1. not (A or B) = not A and not B
  2. not (A and B) = not A or not B

image.png

We can see that everywhere outside the two circles are highlighted. not (A or B) means that it can’t be either A or B, so both A and B should be avoided.

image-2.png

This diagram was tricky for me to understand at first. Basically, it means that the circle with isolated “not A” means that everything outside the two circles AND circle B is “not” A. Therefore, eliminating everything but “A” and “A and B” is what will happen. This also does the same thing with “not B”, as everything but “B” and “A and B” is eliminated.

image-3.png

Example:

!(A > B   B != A)

(A <= B && B == A)

Another Example:

!(A == B   (B >= C   B < A))
(A != B && !(B >=C   B < A))

(A != B && (B < C && B >= A))

Checking Equality in Java:

  • Primitive data types are compared using (a == b)
  • Using == to compare two reference (object) type variables will only check if they are poitning at the same object
  • Using equals() (ex. a.equals(b)). Some classes will check if they are the same object, but some will check if they are equivalent objects
public class CheckingEquality   {
    public static void main(String[] args){
        int a = 3;
        int b = 3;
        double c = 3.0;
        double d = 3.0;
        boolean e = false;

        System.out.println(a == b);
        System.out.println(b == c);
        System.out.println(a != b);
        // System.out.println(c == e); boolean can't be comapred with double value

        
    }
}

CheckingEquality.main(null);
true
true
false

Comparing Objects

  • Compare two objects with ==, but it will only tell us if it is pointing in the same direction
  • We can compare two objects with equals() method
    • “shallow” comparison, only checks whether they are the pointing to the same object (similar to ==)
    • “deep” comparison, only checks whether the data in the two objects are the same
public class ComparingObjects {
    public static void main(String[] args){
        String a = "coffee";
        String b = "coffee"; //in a heap, both object `b` and `a` are directed to the string "coffee" to save memory

        String c = new String("coffee"); //will create a second object in heap
        String d = new String("coffee");

        System.out.println(a == b); //both 'a' and 'b' are pointed to the same memory location
        System.out.println(c == d); //two different memory locations where the data is stored

        System.out.println(a.equals(b)); //true
        System.out.println(a.equals(c)); //false, two different memoring locations
   }
}

ComparingObjects.main(null);
true
false
true
true