Java boolean

The boolean data type in Java is used to store one of two possible values: true or false. It is commonly used when making decisions, checking conditions, and controlling the flow of a program.

For example:

boolean isJavaEasy = true;

Here, the variable isJavaEasy stores the value true.

boolean Example

public class Main {
    public static void main(String[] args) {
        boolean isStudent = true;
        boolean isPassed = false;

        System.out.println(isStudent);
        System.out.println(isPassed);
    }
}

Output:

true
false

boolean with Conditions

Boolean values are commonly used with if statements.

int age = 20;
boolean isAdult = age >= 18;

if (isAdult) {
    System.out.println("You are an adult.");
}

Here, the expression age >= 18 produces a boolean value of true or false.

boolean Operators

Java provides logical operators that work with boolean values:

OperatorMeaning
&&AND
`
!NOT

For example:

boolean result = true && false;

The result is false.

Important Point

Unlike integer types such as int and long, Java does not specify a fixed storage size for boolean values in the Java Language Specification. Therefore, it is better not to describe boolean as exactly 1 bit.

The boolean type is especially useful for conditions, flags, validations, comparisons, and decision-making in Java programs.