Java Booleans
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Java Booleans

Jeevadharshan

Java Booleans

In Java programming, many decisions rely on conditions that evaluate to true or false. The boolean data type is designed specifically for this purpose. It represents logical values and is fundamental to control flow, comparisons, and program logic.

Common real-world representations of boolean values include: 
  • Yes / No 
  • On / Off 
  • Enabled / Disabled 
  • True / False 

What is a Boolean in Java?

A boolean is a primitive data type that can store only two values:
  • true 
  • false  
The term boolean is named after George Boole, whose work on logical algebra forms the foundation of modern computer logic. 

Declaring and Using Boolean Variables 

You declare a boolean variable using the boolean keyword. 

public class BooleanBasics { 
    public static void main(String[] args) { 
        boolean isLoggedIn = true; 
        boolean hasSubscription = false; 
 
        System.out.println("User logged in: " + isLoggedIn); 
        System.out.println("Has subscription: " + hasSubscription); 
    } 
}

Output

User logged in: true 
Has subscription: false 

Boolean Expressions in Java 

A boolean expression is any expression that evaluates to either true or false. These expressions are commonly created using comparison operators such as:
  • > greater than 
  • < less than 
  • >= greater than or equal 
  • <= less than or equal 
  • == equal to 
  • != not equal to  
Example: Comparison Producing Boolean Result

public class AgeCheck { 
    public static void main(String[] args) { 
        int minimumAge = 18; 
        int userAge = 21; 
 
        System.out.println(userAge >= minimumAge); 
    } 
}

Output

true

Direct Boolean Expressions

You can evaluate expressions directly without variables.

public class DirectBoolean { 
    public static void main(String[] args) { 
        System.out.println(25 < 12); 
        System.out.println(7 == 7); 
    } 
}

Output

false 
true

Equality Check Example

public class EqualityCheck { 
    public static void main(String[] args) { 
        int enteredPin = 1234; 
        int actualPin = 1234; 
 
        System.out.println(enteredPin == actualPin); 
    } 
}

Output

true

Storing Comparison Results in Boolean Variables

In real applications, boolean results are often stored for later decisions.

public class BooleanStoreResult { 
    public static void main(String[] args) { 
        double accountBalance = 2500.75; 
        double withdrawalAmount = 1800.00; 
 
        boolean canWithdraw = accountBalance >= withdrawalAmount; 
 
        System.out.println("Withdrawal allowed: " + canWithdraw); 
    } 
}

Output

Withdrawal allowed: true

When Are Booleans Used?

Booleans are essential in:
  • if statements 
  • loops (while, for) 
  • validation checks 
  • authentication logic 
  • feature flags 
  • comparisons and conditions
Example:

public class LoginExample { 
    public static void main(String[] args) { 
        boolean isAuthenticated = true; 
 
        if (isAuthenticated) { 
            System.out.println("Access granted"); 
        } else { 
            System.out.println("Access denied"); 
        } 
    } 
}

Output

Access granted

Tags
Our website uses cookies to enhance your experience. Learn More
Accept !