The Java ternary operator examples

Summary: This tutorial shares examples of the Java ternary operator syntax.

Interested in saying a lot while writing a little? In a single line of code, the Java ternary operator let's you assign a value to a variable based on a boolean expression (either a boolean field, or a statement that evaluates to a boolean result).

At its most basic, the ternary operator (also known as the conditional operator) can be used as an alternative to the Java if/then/else syntax, but it goes beyond that, and can even be used on the right hand side of Java statements.