Java

Booleans in Java explained

The datatypes in Java are categorized into two broader categories. One is primitive and the other is the non-primitive data type. Boolean belongs to the primitive data type of Java. Java Boolean variable takes either true or false value, and thus a Boolean variable or expression plays a vital role in decision making for programmers. This article provides an informative guide about Java Boolean and Java expression. The following are the learning outcomes of this guide:

  • understanding the working of Boolean variable/expression
  • examples that clarify the concept of Boolean in Java

How Boolean works in Java

As discussed earlier, a Boolean variable/expression helps in making a decision between various conditions. The syntax to create a Boolean variable is described below:

boolean variable-name = true/false

In the above syntax,

  • the boolean is the keyword used to declare a Boolean variable in Java
  • the variable-name is user-defined a
  • lastly, it can store only true/false values, therefore the value may be true/false while declaring a Boolean variable.

The above syntax only considers Boolean variables. Whereas the Boolean expression returns the true or false value after going through the condition.

How to use Boolean variable/expression in Java

This section briefly presents few examples that demonstrate the usage of a Boolean variable and expression.

Example 1: Creating a Boolean variable

Following the syntax in the above section, you can create a Boolean variable. For instance, the following statements create two Boolean variables a and b respectively. Moreover, the value assigned to a is true and false value is stored in b variable.

boolean a=true;

boolean b=false;

For better understating, the above statements are used in the following Java code:

package newpack;


public class Boolean {

    public static void main(String[]args) {


        //initializing two boolean variables

        boolean a=true;

        boolean b=false;

       
        //print the variables

        System.out.println("The value of a : " +a);

        System.out.println("The value of b is: " +b);

       
        }


}

The above code is described as:

  • declares two Boolean variables a, b and stores true and false values in them respectively
  • prints the values of a and b

The code written above initializes two boolean variables and then prints them. The screenshot of the output is provided below:

Example 2: Getting the Boolean expression for decision

The primary purpose of the Boolean expression is to assist in making the decision. A Boolean expression returns a Boolean value (true/false). The Java code provided below shows several conditions are tested on two integers and returns the Boolean expression true or false (depending on the true/false of condition).

package newpack;


public class Boolean {

    public static void main(String[]args) {

       
        //initializes two variables

        int a=10, b=7;

       
        //checking various conditions

        System.out.println(a>b);

        System.out.println(a==b);

        System.out.println(a<b);

        }


}

The output of the above code is displayed in the following image:

Example 3: Making use of Boolean operators

The Boolean operators help in comparing multiple conditions on Boolean variables. Several logical operators can be applied to Boolean variables and they are referred to as Boolean operators as well. The following code practices few Boolean operators on Boolean variables to get the result on the basis of decisions made by Boolean operators.

package newpack;


public class Boolean {

    public static void main(String[]args) {

       
        //initializes two boolean variables

        boolean a=true, b=false;

       
        //applying OR(|) operator

        System.out.println(a|b);

       
        //using AND(&) operator

        System.out.println(a&b);

       
        //using NOT(!) and equals(==) operator

        System.out.println(!(a==b));

        }


}

The output of the code is provided below:

  • initializes two Boolean variables
  • applies OR (I) on a and b: The OR operator returns true if one of the a and b values is true.
  • applies AND (&) operator on a and b: The AND operator returns false if one a and b is false.
  • applies NOT (!) and equal (==) operators: As condition a==b is false, and alongside it NOT(reverses the output) operator is used, so the output will be true.

For further guidance, the following table represents how Boolean OR and AND behave against various Boolean values:

Boolean
Value1
Operator Boolean
Value2
output
True | (OR) False True
False | (OR) True True
True | (OR) True True
False | (OR) False False
True & (AND) False False
False & (AND) True False
True & (AND) True True
False & (AND) False False

Conclusion

The Boolean variable in Java stores true or false values whereas a Boolean expression returns a true or false value. These terms are used in Java for decision-making and for checking the various conditions. This post provides the demonstration of Boolean variables and expressions in Java. You would have learned the initialization of Boolean variables. Moreover, we have also provided a few examples that show how Boolean variables and expressions can be useful for decision making.

About the author

Adnan Shabbir