0Pricing
Java Academy · Lesson

Comparison & Logical Operators Deep Dive

Master comparison and logical operators, short-circuiting, precedence, and De Morgan's laws with practical patterns.

Comparisons

Comparison operators

  • ==, !=: equal / not equal
  • <, >, <=, >=: ordering
  • Result is boolean (true or false).

Do not use == for Strings; use equals().

public class Main {
  public static void main(String[] args) {
    int x = 10;
    int y = 20;

    // Compare if x is less than y
    System.out.println(x < y);   // true (10 < 20)

    // Compare if x is equal to y
    System.out.println(x == y);  // false (10 is not equal to 20)

    // Extra examples:
    System.out.println(x > y);   // false (10 > 20? no)
    System.out.println(x != y);  // true  (10 is not equal to 20)
    System.out.println(x <= y);  // true  (10 <= 20)
    System.out.println(x >= y);  // false (10 >= 20? no)
  }
}

Logical Operators

Logical operators

  • && (and)
  • || (or)
  • ! (not)

They combine/negate boolean results. Example: (age >= 18 && country.equals("TR")).

All lessons in this course

  1. Comparison & Logical Operators Deep Dive
  2. Switch Statements
  3. Mini Project: Console Calculator
← Back to Java Academy