Booleans in Java

Boolean Keyword in Java

Java Booleans or boolean keyword in Java is used when you need to inculcate a condition in your program that involves two situations. It is a pre-defined Java data type that takes two values, either true or false.

 

 

Java Booleans Example

To declare a variable of type boolean in Java, the boolean keyword is used before the variable name to which either of the boolean values are assigned which are true and false.

Example of boolean variable declaration in Java:

//Java program to display boolean values -> true/false
public class BooleanVariable {
    public static void main(String[] args){
        //Assigning true boolean value to the boolean variable var_boolean
        boolean var_boolean=true;
        System.out.println("Value of var_boolean is: "+var_boolean);

        //Assigning false boolean value to the boolean variable var_boolean
        var_boolean=false;
        System.out.println("Value of var_boolean is: "+var_boolean);
    }
}

In the above program, a boolean variable “var_boolean” has been created to display how it is created in and used in Java.

Output:

Value of var_boolean is: true
Value of var_boolean is: false

 

 

Boolean Expressions in Java

In Java, boolean expressions return either of the boolean values namely true and false. These expressions make use of relational also know as comparison operators which are less than(<), greater than(>), less than or equal to(<=), greater than or equal to(>=), equal to (==) and not equal to(!=). Comparison operators are literally used when some comparison needs to done in the program. A program is provided below to explain how boolean expressions work and are used in a Java program.

Example of boolean expressions in Java:

//Java program using boolean expressions
public class BooleanExpression {
    public static void main(String args[]){
        int x=23;
        int y=4;
        System.out.println("x>y&&x!=y is "+(x>y&&x!=y));
        System.out.println("x>y&&x==y is "+(x>y&&x==y));
    }
}

In the above program, two boolean expressions have been created which makes use of two variables, 3 different comparison operators and logic and operator. Greater than(>), equal to(==) and not equal to(!=) comparison operators are used above. These expressions are simply displayed to the console and results in either of the boolean values which are true or false.

Output:

x>y&&x!=y is true
x>y&&x==y is false

 

 

Boolean Array in Java

Creating a boolean array in Java is quite easy. It can be used in a program where information regarding the presence and absence of students needs to be stored.

Example of the boolean array in Java:

//Java program that creates a boolean array
public class BooleanArray {
    public static void main(String[] args){
        boolean[] arr_boolean={true,false,true,true};
        for(int i=0;i<4;i++){
            System.out.println(arr_boolean[i]);
        }
    }
}

In the program provided above, a boolean array has been created which stores 4 values -> true, false, true, true in order. These values are then printed on the console using for loop.

 

Output:

true
false
true 
true

 

Also, read, What are Data Types in Java – Primitive and Non-Primitive Data Types?

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *