Boolean Expressions

Basics

Boolean Expressions

🙋 Need help? Ask an expert now!

A "boolean expression" refers to the statement contained inside the brackets of the if statement or the else if statements and only evaluates to either "true" or "false". Remember that an "expression" always consists of literals, operators, variable names, and parentheses used to calculate a value such as true or false. Explore the two boolean expressions below.

BooleanExpressionsExample.java
package exlcode;

public class BooleanExpressionsExample {

  public static boolean exampleVariableOne = true;
  public static boolean exampleVariableTwo = false;

  public static void main(String[] args) {
    if (exampleVariableOne == exampleVariableTwo) {
      System.out.println("The boolean expression is true");
    } else {
      System.out.println("The boolean expression is false");
    }
    // "!=" is the opposite of equals
    if (exampleVariableOne != exampleVariableTwo) {
      System.out.println("The boolean expression is true");
    } else {
      System.out.println("The boolean expression is false");
    }
  }
}

Boolean expressions are used to compare numbers, boolean values, String values, other objects and data types that you will learn about later in the course. Remember the importance of using double equals signs when you're comparing numbers. Please review the sections on "operators" when you need a refresher on the functionality of each one.

Edit Me on GitHub!

Application Question

Given three variables and their values below, which of the following boolean expressions evaluate to true?

boolean varOne = false;
int varTwo  = 2;
int varThree = 20;