Java Boolean Primitive Data Type With Program Examples

For logical values, Java provides boolean keyword, a primitive data type that holds either true or false as possible values.

Relational operators return boolean type values, i.e x > y. Also, control statements that decide the flow of a program such as if statement, use conditional expressions for decision making. These conditional expressions evaluate into boolean types.

It represents 1-bit data which can be true or false. By default, the boolean variable is set to false as a value.

Java Boolean With If statement

If a statement uses a conditional expression that governs the flow. If the conditional expression is evaluated as true, then the if block would be executed, otherwise else statement or code immediately after the if block.

Java program to show the boolean keyword working with If Condition

public class BooleanWithIf {

    public static void main(String[] args) {
        int age = 20;

        if (age > 18) {
            System.out.println("Condition evaluated as true, executing if block");
        } else {
            System.out.println("Condition evaluated as false, executing else block");
        }
    }
}

Output: 

Condition evaluated as true, executing if block

Explanation :

In Above code, if the age variable is carrying 20 as value, if condition checks whether the age is greater than 18 or not, if it’s greater than 18, the if block would be executed, otherwise, else block.

Java Boolean with Relational & Logical Operators

Relational and Logical operators return boolean as required type. Relational operators formulates conditional expressions using operators such as, >,<,==,!==,<=,>= that return boolean as return type. x>y, a==b are the expression examples for this operator.

Logical operators puts  multiple relational expressions together which forms logical expression. It uses operators i.e &&,||,!  for combining the expression and evaluating in term of boolean value. For example, (x>y && a==b).

Java program to show the boolean keyword working with Operators

public class BooleanWithOperators {

    public static void main(String[] args) {
        int x = 10;
        int y= 20;

        System.out.println("relational expression x>y evaluated as = "+ (x<y));
        System.out.println("relational expression x == 0 evaluated as = "+ (x== 0));

        System.out.println("logical expression (x>y && x==0) evaluated as = "+ (x<y && x== 0));

    }
}

Output:

relational expression x>y evaluated as = true
relational expression x == 0 evaluated as = false
logical expression (x>y && x==0) evaluated as = false

Explanation:

  1. x<y,  where x is 10 and y is 20, would be evaluated as true because x is smaller than y
  2. x==0, where x is 10,  would be evaluated as false because x is not equal to 0
  3. (x<y && x==0), for logical AND (&&), it is mandatory to have all the condition evaluated as true, otherwise, it would return always false.

Java Boolean with Comparing Objects

Objects are reference type, when it comes to comparison, operators >,<,== cannot to be used on these types. for this, equal method or comparator is used to achieve comparison functionality.

Java program to show the boolean keyword working with Object

public class BooleanWithObjects {

    public static void main(String[] args) {

        String str =  new String("Boolean");
        String str2 = new String("Boolean");

        System.out.println("comparing two string objects = "+ (str == str2 ));
        System.out.println("comparing two string objects using equal method = "+ (str.equals(str2)));

    }
}

Output: 

comparing two string objects = false
comparing two string objects using equal method = true

Explanation:

Please click here.

 

Frequently Asked Questions:

1. What is a boolean in Java?

Boolean is a primitive data type having size 1 bit that can hold one of the two values, true or false.

2.How to declare a boolean in Java?

A boolean can be declared in two ways, using boolean keyword or Boolean Wrapper class.

Below are the syntax for both the declarations in Java:

boolean variableName = true/false;
Boolean variableName = new Boolean (true/false) // also can carry null value.

3. What is a boolean example?

A boolean is a primitive type used for logical values. Any expression that returns boolean type is considered as logical expression. Relational operators and logical operators are used to form these logical expressions.

Such expression can be viewed as boolean example, i.e x > y, x == y, !x and more.

4. Is boolean a keyword in Java?

Yes, boolean is a keyword used to declare boolean variables in Java.

Leave a Reply