← Chapter 2 — Core Syntax & Data

12. Comparison & Logical Operators

mixed

Description

Comparison operators produce boolean answers. Programs use those answers to decide what should happen next.

Logical operators combine boolean conditions. They are useful when a rule needs more than one check, such as a score and attendance requirement.

Subtopics

Comparisons

Description

Use ==, !=, <, >, <=, and >=.

Explanation

The result is always true or false.

Example

boolean passed = score >= 40;

Logical checks

Description

&& means and, || means or, and ! means not.

Explanation

&& stops early when the first condition is false.

Example

boolean eligible = score >= 40 && attendance >= 75;

Explanation

Use == for primitive equality. For object content, especially String text, use methods such as equals to express the comparison you mean.

Example

public class LogicDemo {
    public static void main(String[] args) {
        int score = 72;
        int attendance = 80;
        boolean eligible = score >= 40 && attendance >= 75;
        System.out.println(eligible);
    }
}

Key points

  • Comparisons return booleans.
  • && requires both conditions.
  • Use equals for String content.

Open reference