Boolean Keyword in Java

In Java, the boolean keyword is a primitive data type. Its size 1-bit of information and it store tow possible Values true or false.

The boolean keyword is used with variables and methods. Its default value is false. It is generally associated with conditional statements.

Examples of Java boolean keyword

public class bool{
  public static void main(String[] args){
    // Using primitive boolean variable:
    boolean x = true;
    if(x)
      System.out.println("x = " + x);
    
    // Using java.lang.Boolean:
    Boolean y = false;
    if(y.toString().equals("false"))
      System.out.println("y = " + y);
  }
}

Output:

x = true
y = false

You can use a comparison operator, such as the greater than (>) operator to find out if an expression (or a variable) is true:

public class MyClass {
  public static void main(String[] args) {
    System.out.println(10 > 9); // returns true, because 10 is higher than 9  
  }
}