Introduction to Java Booleans
In Java, a Boolean is a data type that can hold only two possible values: true or false.
Booleans are the foundation of decision-making in programming. They help your program decide what to do based on certain conditions.
For example, when checking if a user is logged in, a Boolean can be used to represent whether the login is successful (true) or failed (false).
Boolean are wildly used in:
- Conditional statements like if, else if, and else
- Loops like while and for when you want to continue based on a condition
- Logical operations such as AND (&&), OR (||), and NOT (!)
How To Declare and Initialize Booleans In Java?
The syntax of Booleans in Java:
boolean variableName = value;
- Boolean → the data type
- variableName → name you give to the variable
- value → either true or false
Example: Declaring and Initializing Booleans
public class BooleanDemo {
public static void main(String[] args) {
// Declare and initialize boolean variables
boolean hasLicense = true;
boolean isWeekend = false;
// Print their values
System.out.println("Do you have a driving license? " + hasLicense);
System.out.println("Is it weekend today? " + isWeekend);
}
}
- hasLicense is set to true because the person has a driving license.
- isWeekend is set to false because today is not a weekend.
- Using System.out.println(), we can display the Boolean values directly.
Output:
Do you have a driving license? true
Is it weekend today? false
Booleans in Conditional Statements
Booleans are very useful in decision-making. In Java, they are often used in if-else statements to decide which block of code should run based on a condition.
In these statements, the true executes the code inside the if block, and false executes the code inside the else block.
Example: Using Boolean in If-Else
public class BooleanCondition {
public static void main(String[] args) {
boolean isEligible = true;
if (isEligible) {
System.out.println("You are eligible.");
} else {
System.out.println("You are not eligible.");
}
}
}
Explanation:
- hasTicket is a boolean variable representing whether the person has a ticket.
- Since hasTicket = false, the else block executes, and the message “You cannot enter without a ticket.” is printed.
Output:
You cannot enter without a ticket.
Boolean Expressions
These expressions are commonly used in decision-making and loops to control the flow of a program.
Boolean expressions often involve:
- Comparison operators – to compare two values
- Logical operators – to combine multiple conditions
a) Comparison Operators
- ==: Equal to
- !=: Not equal to
- <: Less than
- >: Greater than
- <=: Less than or equal to
- >=: Greater than or equal to
Example: Using Comparison Operators
public class BooleanExpressionExample {
public static void main(String[] args) {
int age = 18;
int votingAge = 21;
// Check if eligible to vote
System.out.println("Is age equal to voting age? " + (age == votingAge)); // false
System.out.println("Is age less than voting age? " + (age < votingAge)); // true
System.out.println("Is age greater than or equal to voting age? " + (age >= votingAge)); // false
}
}
b) Logical Operators
Logical operators are used to combine multiple Boolean expressions or reverse Boolean values. They are especially useful in complex conditions where multiple checks are required.
The three main logical operators in Java are:
- && (AND): Returns true if both conditions are true.
- | | (OR): Returns true if at least one condition is true.
- ! (NOT): Reverses the value of a boolean.
Example: Using Logical Operators
public class LogicalOperatorDemo {
public static void main(String[] args) {
boolean hasID = true;
boolean hasTicket = false;
// AND operator: both conditions must be true
System.out.println("Can enter concert (AND)? " + (hasID && hasTicket)); // false
// OR operator: at least one condition must be true
System.out.println("Can enter concert (OR)? " + (hasID || hasTicket)); // true
// NOT operator: reverses the value
System.out.println("Does not have ID? " + !hasID); // false
}
}
Output:
Can enter concert (AND)? false
Can enter concert (OR)? true
Does not have ID? false
How To Use Booleans in Loops?
Booleans are commonly used in loops to control how long the loop runs.
Example: Using Boolean in a While Loop
public class BooleanLoopDemo {
public static void main(String[] args) {
boolean keepCounting = true;
int number = 1;
while (keepCounting) {
System.out.println("Number: " + number);
number++;
// Stop the loop when number reaches 6
if (number > 5) {
keepCounting = false;
}
}
}
}
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Boolean Methods
In Java, methods can return Boolean values. This is very useful when you want a method to check a condition and tell whether it is true or false.
Example: Boolean Method
public class BooleanMethodDemo {
public static void main(String[] args) {
System.out.println("Is 12 even? " + isEven(12)); // true
System.out.println("Is 17 even? " + isEven(17)); // false
}
// Method returns boolean
public static boolean isEven(int num) {
return num % 2 == 0; // true if divisible by 2
}
}
Explanation:
- isEven checks if a number is divisible by 2.
- If divisible, it returns true; otherwise, it returns false.
Boolean Wrapper Class
Java provides a wrapper class called Boolean for the primitive Boolean type. The Boolean class is part of the java.lang package and offers useful methods, such as:
- Boolean.valueOf(boolean) → Converts a primitive Boolean to a Boolean object
- Boolean.parseBoolean(String) → Converts a String like “true” or “false” to a Boolean
Example: Boolean Wrapper Class
public class BooleanWrapperDemo {
public static void main(String[] args) {
// Convert primitive boolean to Boolean object
Boolean isActive = Boolean.valueOf(true);
System.out.println("Boolean object: " + isActive); // true
// Convert String to boolean primitive
boolean parsedValue = Boolean.parseBoolean("true");
System.out.println("Parsed boolean: " + parsedValue); // true
}
}
In this code:
- Boolean.valueOf(true) creates a Boolean object from a primitive.
- Boolean.parseBoolean(“true”) converts a String to a primitive Boolean.
- These methods are useful when working with collections, APIs, or input data where Booleans may come as objects or strings.
Exercise: Student Exam Eligibility Checker
Create a Java program that checks whether a student is eligible to take a final exam based on the following conditions:
The student must have at least 75% attendance, submit all assignments, and must not have any disciplinary issues.
Your program should:
- Ask the user to input the three conditions using Boolean values (true or false).
- Use Boolean expressions and logical operators to determine eligibility.
- Print whether the student is eligible or not eligible for the exam.
Simple soultion:
import java.util.Scanner;
public class ExamEligibilityChecker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input conditions from user
System.out.print("Has the student attended 75% or more classes? (true/false): ");
boolean hasAttendance = sc.nextBoolean();
System.out.print("Has the student submitted all assignments? (true/false): ");
boolean assignmentsSubmitted = sc.nextBoolean();
System.out.print("Does the student have any disciplinary issues? (true/false): ");
boolean noDisciplinaryIssues = sc.nextBoolean();
// Check eligibility using boolean expressions
boolean isEligible = hasAttendance && assignmentsSubmitted && noDisciplinaryIssues;
// Output result
if (isEligible) {
System.out.println("The student is eligible for the final exam.");
} else {
System.out.println("The student is NOT eligible for the final exam.");
}
sc.close();
}
}
- Run this code in your terminal and understand Java Boolean concept.
Also Learn Important Topics of Java
- What are the operators in Java Programming?
- What are Data Types in Java?
- What are the Variables in Java?
- Learn Java syntax.

M.Sc. (Information Technology). I explain AI, AGI, Programming and future technologies in simple language. Founder of BoxOfLearn.com.