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.

Comparison & Logical Operators Deep Dive is a free Java Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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")).

Short-circuit

Short-circuit

  • a && b: if a is false, b is not evaluated.
  • a || b: if a is true, b is not evaluated.
  • Use this to guard checks that could fail (e.g., null checks).

Precedence

Precedence

  • Comparisons happen before && and ||.
  • && binds tighter than ||.
  • Use parentheses for clarity: (a > 0 && b > 0) || c > 0.

Patterns

De Morgan's laws

  • !(a && b)!a || !b
  • !(a || b)!a && !b

Range checks

  • 10 <= x && x <= 20 (inclusive)
  • x < 10 || x > 20 (outside)

Operators Demo

Try it: Enter values for x and y. Observe comparison results, combined conditions, and a short-circuit null guard.

public class Main {
  public static void main(String[] args) {
    // Instead of reading from Scanner, we assign fixed values
    int x = 12;
    int y = 5;

    boolean cmp1 = x < y;
    boolean cmp2 = x == y;
    boolean bothPositive = (x > 0 && y > 0);
    boolean eitherZero = (x == 0 || y == 0);
    boolean inRangeX = (10 <= x && x <= 20);

    System.out.println("x < y: " + cmp1);
    System.out.println("x == y: " + cmp2);
    System.out.println("bothPositive: " + bothPositive);
    System.out.println("eitherZero: " + eitherZero);
    System.out.println("10 <= x <= 20: " + inRangeX);

    // Short-circuit guard example
    String s = (x > 5) ? "big" : null;
    if (s != null && s.equals("big")) {
      System.out.println("s is big");
    }
  }
}

Short-circuit Check

Quick check: Which statements about short-circuit evaluation are true?

Recap & Next

Recap: You practiced comparisons, logical operators, short-circuiting, precedence, and De Morgan's laws.

Next: Use switch to select from multiple options more cleanly.

Frequently asked questions

Is the “Comparison & Logical Operators Deep Dive” lesson free?

Yes — the full text of “Comparison & Logical Operators Deep Dive” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Comparison & Logical Operators Deep Dive”?

Master comparison and logical operators, short-circuiting, precedence, and De Morgan's laws with practical patterns. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Comparison & Logical Operators Deep Dive” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

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