Introduction
In this lab, you will learn how to check if a boolean variable is false in Java. We will explore fundamental techniques for working with boolean values, which are essential for controlling program flow based on conditions.
Through hands-on examples, you will practice using the equality operator (==) to check for false, test with Boolean objects, and combine checks for both true and false conditions. By the end of this lab, you will have a solid understanding of how to effectively utilize boolean variables in your Java programs.
Use Equality Operator for False Check
In this step, we will explore how to check if a boolean variable is false using the equality operator (==) in Java. Understanding how to work with boolean values is fundamental in programming, as they are used to control the flow of your programs based on conditions.
A boolean variable can hold one of two values: true or false. These values are often the result of a comparison or a logical operation.
Let's create a simple Java program to demonstrate checking for false.
Open the
HelloJava.javafile in the WebIDE editor if it's not already open.Replace the existing code with the following:
public class HelloJava { public static void main(String[] args) { boolean isComplete = false; if (isComplete == false) { System.out.println("The task is not complete."); } } }Let's look at the new parts:
boolean isComplete = false;: This line declares a boolean variable namedisCompleteand initializes it with the valuefalse.if (isComplete == false): This is anifstatement, which is used to execute a block of code only if a certain condition is true. The condition here isisComplete == false. The==operator checks if the value ofisCompleteis equal tofalse.System.out.println("The task is not complete.");: This line will be executed only if the conditionisComplete == falseis true.
Save the file (Ctrl+S or Cmd+S).
Compile the program using the
javaccommand in the Terminal:javac HelloJava.javaIf there are no errors, you will see no output.
Run the compiled program using the
javacommand:java HelloJavaYou should see the following output:
The task is not complete.This output confirms that the condition
isComplete == falsewas indeed true, and the code inside theifblock was executed.
Using the equality operator (==) to check if a boolean is false is a straightforward way to express this condition. In the next step, we will explore another way to check for the false value.
Test with Boolean Object
In the previous step, we worked with a primitive boolean type. Java also has a corresponding class called Boolean, which is an object wrapper for the primitive boolean type. While you'll often use the primitive type, it's useful to know about the Boolean object, especially when working with collections or methods that require objects.
In this step, we'll see how to check for the false value when using a Boolean object.
Open the
HelloJava.javafile in the WebIDE editor.Replace the current code with the following:
public class HelloJava { public static void main(String[] args) { Boolean isProcessed = Boolean.FALSE; if (isProcessed.equals(false)) { System.out.println("The item has not been processed."); } } }Let's look at the changes:
Boolean isProcessed = Boolean.FALSE;: This line declares aBooleanobject namedisProcessedand initializes it with the static constantBoolean.FALSE, which represents the boolean valuefalse.if (isProcessed.equals(false)): When working with objects in Java, it's generally recommended to use theequals()method to compare their values, rather than the==operator. Theequals()method of theBooleanclass checks if the object's boolean value is equal to the boolean value passed as an argument.
Save the file (Ctrl+S or Cmd+S).
Compile the program using
javacin the Terminal:javac HelloJava.javaAgain, no output means successful compilation.
Run the program using
java:java HelloJavaYou should see the following output:
The item has not been processed.This shows that the
equals()method correctly identified that theBooleanobjectisProcessedholds the boolean valuefalse.
While using == false with primitive booleans is common and perfectly fine, using .equals(false) is the standard way to compare Boolean objects. Understanding the difference between primitive types and their object wrappers is an important concept in Java.
Combine True and False Checks
In real-world programming, you'll often need to check multiple conditions simultaneously. This involves combining checks for both true and false values using logical operators like && (AND) and || (OR).
In this step, we'll modify our program to include checks for both true and false conditions.
Open the
HelloJava.javafile in the WebIDE editor.Replace the current code with the following:
public class HelloJava { public static void main(String[] args) { boolean isTaskStarted = true; boolean isTaskFinished = false; if (isTaskStarted == true && isTaskFinished == false) { System.out.println("The task has started but is not finished."); } if (isTaskStarted == false || isTaskFinished == true) { System.out.println("The task is either not started or is already finished."); } } }Let's break down the new code:
boolean isTaskStarted = true;: We declare a boolean variableisTaskStartedand set it totrue.boolean isTaskFinished = false;: We declare another boolean variableisTaskFinishedand set it tofalse.if (isTaskStarted == true && isTaskFinished == false): Thisifstatement uses the logical AND operator (&&). The code inside this block will execute only if both conditions are true:isTaskStartedistrueANDisTaskFinishedisfalse.if (isTaskStarted == false || isTaskFinished == true): Thisifstatement uses the logical OR operator (||). The code inside this block will execute if at least one of the conditions is true:isTaskStartedisfalseORisTaskFinishedistrue.
Save the file (Ctrl+S or Cmd+S).
Compile the program using
javacin the Terminal:javac HelloJava.javaRun the program using
java:java HelloJavaBased on the initial values of
isTaskStartedandisTaskFinished, you should see the following output:The task has started but is not finished.The first
ifcondition (true && false) evaluates tofalse, so the firstprintlnis executed. The secondifcondition (true || false) evaluates totrue, so the secondprintlnis executed.Wait, the output is only "The task has started but is not finished."? Let's re-evaluate the conditions:
isTaskStarted == true && isTaskFinished == false:true == trueistrue,false == falseistrue.true && trueistrue. So the first message prints.isTaskStarted == false || isTaskFinished == true:true == falseisfalse,false == trueisfalse.false || falseisfalse. So the second message does not print.
My apologies, the expected output is indeed only the first line. This demonstrates how logical operators work to combine conditions.
You can experiment by changing the initial values of
isTaskStartedandisTaskFinishedand re-running the program to see how the output changes.
Combining boolean checks with logical operators is a powerful way to control the flow of your programs and make decisions based on multiple factors.
Summary
In this lab, we learned how to check if a boolean variable is false in Java. We started by using the equality operator (==) to compare a boolean variable directly with the false literal. This demonstrated the fundamental way to check for a false condition and control program flow using an if statement. We then tested this concept with a simple Java program, compiling and running it to observe the expected output when the boolean variable was indeed false.



