Syntax

Java Ternary Operator Syntax

In Java, ternary operator: picks one of two values in one line — String s = ok ? "yes" : "no";

Quick Answer

Syntax
String s = ok ? "yes" : "no";
JavaSyntax

Ternary Operator

Code example
String s = ok ? "yes" : "no";

Picks one of two values in one line.

Learn More

How to ternary operator in Java

  1. Open your Java file or editor.
  2. Copy the example, then replace its sample names and values for your code.

Java Ternary Operator Syntax

Use String s = ok ? "yes" : "no";.

Related Syntax References

Frequently asked questions

What is the Java syntax for ternary operator?

Use this pattern: String s = ok ? "yes" : "no";

Can I copy this Java code example?

Yes. Use Copy code and adapt the example to your program.

Is this Java syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify