Once you click post assignment, learners in the selected class will immediately be able to see and complete this assignment
Back to Classroom
Boolean Expressions
Java Selection Statements Course
Engineers From These Top Companies and Universities Trust EXLskills
1M+ Professionals | 100+ Institutions
This is the EXLskills free and open-source Java Selection Statements Micro Course. It guides learners via explanation, demonstration, and thorough practice, from no more than a basic understanding of Java, to a moderate level of understanding regarding Java selection statements.
Is this course FREE?
Yes, this a 100% free course that you can contribute to on GitHub here!
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.
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;