Java Lesson — Boolean, Relational Operators, and Logical Operators

Learn how Java makes decisions using true, false, comparisons, and logical conditions.

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.

Boolean

A value that is either true or false

Relational Operators

Used to compare two values

Logical Operators

Used to combine or reverse conditions

Decision Making

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 ==.

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

Assignment 1: Create boolean variables for isPassed and isPresent. Print both values.
Assignment 2: Compare two integers using all six relational operators and print the results.
Assignment 3: Write a program that checks whether a number is positive using >.
Assignment 4: Write a program that checks whether a student's marks are greater than or equal to 40. Print pass or fail.
Assignment 5: Create two boolean variables and use &&, ||, and ! on them.
Assignment 6: Write a program that checks whether a person can vote. The condition is age greater than or equal to 18.
Assignment 7: Write a program that allows entry only if age is at least 18 and the person has an ID card.
Assignment 8: Write a program that checks whether a day is a weekend using OR logic for Saturday or Sunday.

Interactive MCQ Quiz

Choose one answer for each question and click Check Answers.

Total Questions: 0

0 / 0

Your result will appear here.

Quick revision