Skip to main content

Command Palette

Search for a command to run...

Ternary operator | Java

Basics • Java Fundamentals

Updated
1 min read
Ternary operator | Java
X

Myself Vikas Singh from Varanasi, Uttar Pradesh. Learning and exploring technical domains at Acharya Institute, Bangalore (IN) from the last two years.

The main goal is to learn as much domains, tool

  • Represented by ?:

  • Operator have more than three operand are called ternary operators.

  • The only possible ternary operator in Java is conditional operator.

Syntax:

expression ? true : false

If expression is true then result is will be true otherwise false.

Example -

int x = (10 < 20) ? 30 : 40;
System.out.println(x); // 30

We can perform nesting of conditional operator also.

int x = (10 > 20) ? 30 : ((40 > 30) ? 70 : 60);
System.out.println(x); // 70

Conclusion

The ternary operator in Java offers a concise way to express conditional statements. Mastering its usage enhances code readability and simplifies decision-making, contributing to efficient and expressive programming.

Java

Part 2 of 16

Explore the richness of Java SE 8 in this article series! Dive into powerful features like fundamentals, lambdas, streams, and more, transforming your Java development experience.

Up next

Mastering Java Transfer Statements: Streamlining Control Flow in Your Code

Flow control • Java Fundamentals