-
Hajipur, Bihar, 844101
In Java, a Boolean is a simple data type that can hold only two possible values: true or false. These values represent logical conditions and play an important role in decision-making processes in a program. You’ll often see Booleans used in conditional statements, loops, and comparisons where a program needs to choose between two paths.
For example, when checking if a user is logged in or not, or whether a number is greater than another, Boolean expressions help your program make such logical decisions automatically.
The Boolean data type in Java is declared using the keyword boolean. It can store only one bit of information—either true or false. Even though it looks simple, Booleans are the foundation of all logical operations in programming.
Example:
boolean isJavaFun = true;
boolean isRaining = false;
System.out.println(isJavaFun); // Output: true
System.out.println(isRaining); // Output: false
Here, two Boolean variables are created. The first one holds true, while the second holds false.
A Boolean expression is an expression that returns a Boolean value—either true or false. These expressions are usually formed using comparison operators like <, >, ==, !=, <=, or >=.
Example:
int a = 10;
int b = 20;
System.out.println(a < b); // Output: true
System.out.println(a == b); // Output: false
The comparison a < b results in true, while a == b results in false. Java evaluates these expressions and returns the Boolean result.
Booleans are most commonly used in conditional statements such as if, else if, and while. These structures check whether a Boolean condition is true and execute code accordingly.
Example:
boolean isStudent = true;
if (isStudent) {
System.out.println("Access granted to the student portal.");
} else {
System.out.println("Access denied.");
}
Here, the message “Access granted to the student portal” will be printed because the condition evaluates to true.
You can also directly use comparison expressions instead of Boolean variables:
int marks = 85;
if (marks >= 40) {
System.out.println("You passed the exam!");
} else {
System.out.println("You failed.");
}
Java provides logical operators that work with Boolean values. These operators are often used to combine multiple conditions.
| Operator | Description | Example |
|---|---|---|
&& |
Logical AND – returns true if both conditions are true | (x > 5 && y < 10) |
| ` | ` | |
! |
Logical NOT – reverses the Boolean value | !(x > 5) |
Example:
int age = 25;
boolean hasLicense = true;
if (age >= 18 && hasLicense) {
System.out.println("You can drive a car.");
} else {
System.out.println("You cannot drive yet.");
}
Apart from the primitive boolean type, Java also provides a Boolean class in the java.lang package. It wraps the primitive type into an object and provides several useful methods.
Example:
Boolean boolObj = Boolean.valueOf(true);
System.out.println(boolObj.booleanValue()); // Output: true
Common Boolean class methods:
booleanValue() – returns the primitive boolean value
compareTo() – compares two Boolean objects
equals() – checks equality of two Boolean objects
toString() – converts a Boolean value to a string representation
Example:
Boolean x = true;
Boolean y = false;
System.out.println(x.equals(y)); // false
System.out.println(x.compareTo(y)); // 1
System.out.println(x.toString()); // "true"
In Java, methods can return Boolean values. This is useful when a method needs to confirm if a certain condition is met.
Example:
public class Example {
static boolean isEven(int num) {
return num % 2 == 0;
}
public static void main(String[] args) {
System.out.println(isEven(10)); // true
System.out.println(isEven(7)); // false
}
}
Here, the method isEven() checks if a number is divisible by 2 and returns true if it is, otherwise false.
Booleans are used everywhere in software systems. Here are some examples:
Checking user authentication (isLoggedIn)
Validating inputs (isValidEmail)
Game states (isGameOver)
Toggle settings (isDarkMode)
Payment status (isPaid)
These variables make the code more readable and logical.
Here’s a complete example that uses Booleans in different ways:
public class BooleanExample {
public static void main(String[] args) {
int temperature = 30;
boolean isHot = temperature > 25;
boolean isRaining = false;
if (isHot && !isRaining) {
System.out.println("Let's go for a walk.");
} else if (isHot && isRaining) {
System.out.println("Stay home, it's humid outside.");
} else {
System.out.println("It's cool and pleasant today.");
}
}
}
This example checks the temperature and weather to decide what to do. It uses Boolean conditions, logical operators, and if-else statements together.
Booleans are one of the simplest yet most powerful concepts in Java. They help your programs make logical decisions and control how the code flows. Whether you’re comparing values, validating conditions, or using loops, Boolean values ensure that only the right blocks of code are executed. Understanding how to use them properly lays a strong foundation for writing smart, interactive, and reliable Java applications.
Write a Java program that declares two Boolean variables, one for isRaining and one for isSunny, and prints whether it is a good day for a picnic using logical operators.
Create a Java program that checks whether a given number is even or odd using a method that returns a Boolean value. Print “Even” if true and “Odd” if false.
Write a program that takes two integer inputs and checks if the first number is greater than or equal to the second using a Boolean expression. Display the Boolean result.
Develop a program that uses Boolean logic to determine if a person is eligible to vote based on their age and citizenship status.
Write a Java program that checks if a given year is a leap year or not using Boolean conditions and prints the result.
Create a program that defines a method isPrime() that returns a Boolean indicating whether the entered number is prime. Call this method from main() and display the result.
Write a program that uses the Boolean class and demonstrates the use of equals(), compareTo(), and toString() methods with Boolean objects.
Develop a Java program that uses logical operators to check if a user has both a valid username and password before allowing login.
Write a program that determines whether a given temperature indicates “Hot,” “Cold,” or “Moderate” using multiple Boolean conditions and an if-else ladder.
Create a Java program that takes three Boolean inputs: hasID, hasTicket, and isVIP. Print whether the person is allowed entry based on these conditions using logical AND and OR operators.