Conditionals
Conditionals choose which statements run. if, else if, and else test a boolean and take one branch. The other branches are skipped.
Detailed Explanation
A program that always runs the same lines cannot react. You need a yes-or-no question: is the cart empty, is the user an admin, did the parse succeed. In Java that question is a boolean. if (ready) runs the next statement or block only when ready is true. else covers the rest. else if chains extra tests in order; the first true test wins and the later ones do not run.
The test must be a boolean. Java will not treat 1 or a non-null object as true. That is unlike C, Python, and JavaScript. You write the comparison yourself: count > 0, name != null, status == Status.OPEN. == on objects is identity, so strings use equals. For enums, == is correct.
Braces are optional for a single statement and required for more than one. Always write the braces. A later edit that adds a line without braces attaches that line to the outer block, and the if no longer guards it. That class of bug has shipped in production more than once.
There is no elif keyword. Nesting is just else followed by another if. Deep nests are hard to read. Prefer an early return when a guard fails (if (user == null) return;) so the rest of the method stays flat. Prefer a switch when you are matching one value against many constants. Prefer polymorphism when the branch depends on type and would otherwise grow with every new subtype.
The ternary operator cond ? a : b is an expression, not a statement. Both arms must produce a value, and they must share a type the compiler can pick. Use it for short choices (name != null ? name : "anon"). Do not nest ternaries. An if is clearer once either arm needs a comment.
Boolean operators short-circuit. a && b does not evaluate b if a is false. a || b does not evaluate b if a is true. That is why user != null && user.active() is safe and user.active() && user != null is not. Bitwise & and | on booleans do not short-circuit; leave them for flags, not guards.
Code Examples
public class Access {
static String gate(boolean admin, int age) {
if (admin) {
return "all";
} else if (age >= 18) {
return "standard";
} else {
return "blocked";
}
}
public static void main(String[] args) {
System.out.println(gate(false, 20)); // prints: standard
System.out.println(gate(true, 12)); // prints: all
}
}public class Guards {
static int length(String name) {
if (name == null || name.isBlank()) { // isBlank is not called when name is null
return 0;
}
return name.length();
}
public static void main(String[] args) {
System.out.println(length(null)); // prints: 0
System.out.println(length("Ira")); // prints: 3
}
}Common Gotchas
A non-boolean test does not compile
`if (count)` is illegal. Write `if (count != 0)`.
Missing braces attach the next line to the outer block
always wrap the body in `{ }`.
`==` on `String` compares references
use `Objects.equals(a, b)` or `a.equals(b)` after a null check.
`&&` and `||` skip the right-hand side
put the cheap, null-safe check first.