Language syntax

Conditional operator ?:

Selects one of two result expressions after evaluating a boolean condition. Only the chosen result expression executes. The resulting type follows conditional-expression typing rules; matching branch types are easiest to reason about.

Formal Syntax & Structure

java syntaxjava
condition ? whenTrue : whenFalse

Working Example

Example Snippetjava
public class Main {    public static void main(String[] args) {        int stock = 0;        System.out.println(stock > 0 ? "In stock" : "Sold out");    }}
Java Reference