What this lesson covers
In this lesson, you will learn what a boolean value is in Java, how
relational operators compare values, and how logical operators
combine multiple conditions. These ideas are extremely important
because they are used in if statements, loops, input
validation, and decision making.
Once you understand booleans and operators like >,
<, ==, &&,
||, and !, you can start writing real
programs that react to user input and make smart choices.
A value that is either true or false
Used to compare two values
Used to combine or reverse conditions
Used in if statements and loops
What is a boolean?
A boolean in Java is a data type that stores only one of two possible
values:
true or false.
boolean isJavaFun = true; boolean isFishFlying = false; System.out.println(isJavaFun); System.out.println(isFishFlying);
Boolean values are very useful when you want to test a condition, such as whether a student passed, whether a number is even, or whether a user is allowed to log in.
Relational Operators
Relational operators compare two values and produce a boolean result.
That means the answer will always be either true or
false.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== |
Equal to | 5 == 5 |
true |
!= |
Not equal to | 5 != 3 |
true |
> |
Greater than | 7 > 2 |
true |
< |
Less than | 4 < 9 |
true |
>= |
Greater than or equal to | 6 >= 6 |
true |
<= |
Less than or equal to | 3 <= 5 |
true |
Examples
int a = 10; int b = 20; System.out.println(a == b); // false System.out.println(a != b); // true System.out.println(a < b); // true System.out.println(a > b); // false System.out.println(a <= 10); // true System.out.println(b >= 15); // true
These operators do not change the values. They only compare them and return a boolean result.
Logical Operators
Logical operators are used when you want to combine two or more boolean conditions, or when you want to reverse a condition.
| Operator | Meaning | Example | Result |
|---|---|---|---|
&& |
Logical AND | (5 > 2) && (8 > 4) |
true |
|| |
Logical OR | (5 < 2) || (8 > 4) |
true |
! |
Logical NOT | !(5 > 2) |
false |
How AND works
The && operator returns true only
when both conditions are true.
int age = 20; boolean hasIdCard = true; System.out.println(age >= 18 && hasIdCard); // true
How OR works
The || operator returns true if at least one
condition is true.
boolean isHoliday = false; boolean isSunday = true; System.out.println(isHoliday || isSunday); // true
How NOT works
The ! operator reverses the boolean value. It changes
true to false and false to
true.
boolean isRaining = false; System.out.println(!isRaining); // true
Using booleans and operators in if statements
These operators become very useful when you write decision-making code.
int marks = 75;
if (marks >= 40) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
int age = 22;
boolean hasTicket = true;
if (age >= 18 && hasTicket) {
System.out.println("You may enter.");
} else {
System.out.println("Entry not allowed.");
}
Important beginner note: = versus ==
Many beginners confuse = and ==.
-
=means assignment. It stores a value in a variable. -
==means comparison. It checks whether two values are equal.
int x = 5; // assignment System.out.println(x == 5); // comparison, result is true
This difference is extremely important in Java programming.
Complete example
public class Main {
public static void main(String[] args) {
int age = 19;
int marks = 68;
boolean hasAdmitCard = true;
System.out.println("age >= 18: " + (age >= 18));
System.out.println("marks >= 40: " + (marks >= 40));
System.out.println("hasAdmitCard: " + hasAdmitCard);
if (marks >= 40 && hasAdmitCard) {
System.out.println("Student can appear in the next step.");
} else {
System.out.println("Student cannot appear in the next step.");
}
if (age < 18 || marks > 60) {
System.out.println("At least one special condition is true.");
}
System.out.println("Not having admit card: " + !hasAdmitCard);
}
}
Assignments
isPassed and isPresent. Print both values.
>.
&&, ||, and ! on them.
Interactive MCQ Quiz
Choose one answer for each question and click Check Answers.
0 / 0
Your result will appear here.
Quick revision
-
A boolean stores only
trueorfalse. - Relational operators compare values and return boolean results.
-
==checks equality, while=assigns a value. -
&&means both conditions must be true. -
||means at least one condition must be true. !reverses a boolean value.-
These operators are heavily used in
ifstatements and loops.