Conditionals in Java
Control program flow with if/else, traditional switch statements, and modern switch expressions (Java 14+) with arrow syntax and yield.
if, else if, else
The `if` statement is the most fundamental way to make decisions in Java. It executes a block of code only when a condition is `true`. Chain `else if` for multiple branches, and end with `else` as a catch-all fallback.
1. Basic if Statement
Execute a block only when the condition is true.
2. if / else if / else Chain
Test multiple conditions in order. The first branch that matches runs; all others are skipped.
3. Nested if Statements
Place an `if` inside another `if` to check compound conditions step by step.
switch Statement (Classic)
The classic `switch` statement tests a single variable against a list of `case` values. Each `case` must end with a `break` to prevent fall-through into the next case. The `default` block runs when no case matches.
1. Basic switch Statement
Match an integer, String, or enum value against a set of cases.
2. Fall-Through (Intentional)
Omitting `break` causes execution to continue into the next case — useful for grouping cases.
3. switch on String
Since Java 7, `switch` can match on `String` values — useful for command routing and status handling.
switch Expression (Java 14+)
Java 14 (JEP 361) made switch expressions a standard feature. The arrow `->` syntax eliminates fall-through completely and allows `switch` to return a value directly. Use `yield` when a case body needs multiple statements.
1. Arrow Syntax (No Fall-Through)
Replace `case X: ... break;` with `case X ->` for cleaner, safer code. Java 14+.
2. Multiple Labels per Case
Group several values into one arrow case using a comma-separated list.
3. yield — Multi-Statement Cases
When a case body needs more than one statement, use a block `{ }` and return the value with `yield`.
4. Exhaustive switch with Enums (Java 21+)
When switching over an `enum`, Java 21+ pattern matching can enforce exhaustiveness at compile time.
Pattern Matching in switch (Java 21+)
Java 21 (JEP 441) made pattern matching for switch a standard feature. You can match an `Object` against type patterns, guarded patterns (`when`), and `null` — eliminating chains of `instanceof` checks.
1. Type Patterns
Match an object against multiple types in one switch instead of chaining instanceof checks.
2. Guarded Patterns (when)
Add a condition to a type pattern using `when` to further filter within a case.
Ternary Operator
The ternary operator `condition ? valueIfTrue : valueIfFalse` is the only operator in Java that takes three operands. It is an expression — it produces a value — making it useful inside assignments, return statements, and print calls.
1. Ternary Basics
Replace a simple if-else assignment with a single concise expression.
2. Ternary for Null Safety
A common pattern: provide a default value when a variable might be null.
3. Nested Ternary (Use Carefully)
Nest ternaries for compact multi-branch logic. Add parentheses to make the structure clear.
Truthy & Falsy in Java
Unlike JavaScript, Java does not have implicit truthy/falsy values. Every `if` condition must be a strict `boolean` expression. However, understanding which values compare as "truthy-like" (non-zero, non-null, non-empty) is essential for writing correct conditionals.
1. Java Requires Strict boolean
In Java, you cannot use an integer or object directly as a condition — it must be a `boolean` expression.
2. "Falsy-like" Values in Java
These are the values Java developers treat as false-equivalent when writing conditions.
Guard Clauses
A guard clause is an early `return`, `throw`, or `continue` that handles edge cases at the top of a method or loop body. This keeps the main logic un-nested and easy to follow — the "happy path" is always at the lowest indentation level.
1. Guard Clause in Methods
Return early for invalid or edge-case inputs instead of wrapping the main logic in a deep if-else.
2. Guard Clause in Loops (continue)
Use `continue` inside a loop to skip invalid items early instead of wrapping loop body in an `if`.
Best Practices
Following these practices prevents common bugs, reduces nesting, and makes conditional logic easy to understand and maintain.
1. Recommended Best Practices
Key rules for writing clean conditionals in Java.