Precedence, De Morgan and the Integer cache
The -128 to 127 cache is the default; it can be raised with -XX:AutoBoxCacheMax, which is one more reason never to rely on == for Integer.
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.
Print whether each of the years 1900, 2000, 2024 and 2026 is a leap year.
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.
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
What does short-circuit evaluation mean, and give a real example where it prevents a crash.
Why does == work for int but not reliably for String or Integer?
What is the value of true || false && false, and why?
Negate x >= 1 && x <= 10 correctly and explain the rule you used.
Expected output
a || b && c = true
(a || b) && c = false
De Morgan holds: true
true ^ true = false
in range: true, out of range: false
Integer 127 == 127: true
Integer 128 == 128: false
128 equals 128: true