Command Palette

Search for a command to run...

Back to the lesson: Topic 1.8 — Comparison and Logical Operators
Core Java · Example 1 of 3

Comparisons produce booleans

Comparison operators (==, !=, <, >, <=, >=) ask a yes/no question and produce a boolean; logical operators (&&, ||, !) combine those answers. && and || short-circuit, skipping the right side when the left already decides the result, which is both an optimisation and a safety tool.

Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Print whether each of the years 1900, 2000, 2024 and 2026 is a leap year.

02

A shop gives free delivery to members, or to anyone whose order is at least 500, but never to blocked customers. Write the condition and test three customers.

03

Given String s = null, print "empty" if s is null or has length 0, and "has text" otherwise, without crashing. Then test with "hi".

Explain it without notes

01

What does short-circuit evaluation mean, and give a real example where it prevents a crash.

02

Why does == work for int but not reliably for String or Integer?

03

What is the value of true || false && false, and why?

04

Negate x >= 1 && x <= 10 correctly and explain the rule you used.

Comparisons produce booleans
Sign in to run this example in your browser.

Expected output

age > 12:  true
age >= 18: false
age == 15: true
age != 15: false
10 == 10.0: true
'a' == 97:  true
'a' < 'b':  true
NaN < 1 || NaN >= 1: false
adult: false