Yes, this is correct syntax but it's not readable.

You can check by yourself this in Java. Like this:

int a = 3;
int b = 5;
String s = (a < b) ? "Less than b" : (a > b) ? "More than b" : "Equal as b";
System.out.println(s);

But code is much more readable if you use if and if else statements. This ? and : is just for basic if statement. For example:

int a = 3;
int b = 5;
String s = (a == b) ? "Equal" : "Not equal"
System.out.println(s);

But even in this case, I would rather use if statement. I really don't like to see ? and : instead of if statement :)

Regards, golobic

Answer from golobitch on Stack Overflow
🌐
Codespindle
codespindle.com › Java › Java_Ternary.html
Ternary Operator and Nested Ternary Operators in Java
The following example shows how ... true : false; System.out.println(isAdult); // Prints "true" The ternary operator can be nested to create more complex conditional statements....
🌐
Programiz
programiz.com › java-programming › ternary-operator
Java Ternary Operator (With Example)
It is also possible to use one ternary operator inside another ternary operator. It is called the nested ternary operator in Java.
🌐
Baeldung
baeldung.com › home › java › core java › ternary operator in java
Ternary Operator in Java | Baeldung
September 24, 2025 - We can nest our ternary operator to any number of levels of our choice.
🌐
Coderanch
coderanch.com › t › 388475 › java › Nested-Ternary-operators
Nested Ternary operators (Beginning Java forum at Coderanch)
To evaluate it, you have to realize that the ternary operator is one of the few operators in Java that has right-to-left associativity. In other words, when confronted with nested ternary operators, Java starts evaluating the rightmost one and works its way left from there.
🌐
Hero Vired
herovired.com › learning-hub › blogs › ternary-operator-java
Ternary Operator in Java with Examples | Hero vired
Developers can use the Java ternary operator as a substitution for if-else statements. Also, they can create switch statements with nested ternary operators.
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-Ternary-Operator-Nested-Examples-Return-if-else-symbol-void-null
How to use Java's conditional operator ?:
See if you can figure out the logic behind the following nested ternary operator example: var players = 9; var result = (players==11)? "baseball" : ((players==9) ? "footie" : "darts"); The logic of the nested Java ternary operator example works like this:
Find elsewhere
🌐
YouTube
youtube.com › watch
Java Programming - Ternary Operator - Nested Ternary Operator - Exercises - CSE1007 - YouTube
Code can be downloaded from https://codespindle.com/Java/Java_Ternary.htmlIn this session the following concepts are covered1. What is a Ternary Operator?2. ...
Published   July 13, 2020
Views   5K
🌐
GeeksforGeeks
geeksforgeeks.org › java-ternary-operator-with-examples
Java Ternary Operator with Examples | GeeksforGeeks
Java ternary operator is the only conditional operator that takes three operands. It’s a one-liner replacement for the if-then-else statement and is used a lot in Java programming.
Published   January 4, 2025
🌐
Scaler
scaler.com › home › topics › java › ternary operator in java
Ternary Operator in Java - Scaler Topics
July 19, 2023 - The nested ternary operator is an extension of the ternary operator in Java, allowing for multiple levels of conditionals within a single expression.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-ternary-operator
Java Ternary Operator - GeeksforGeeks
The ternary operator is a compact alternative to the if-else statement. It evaluates a condition and returns one of two values depending on whether the condition is true or false.
Published   December 20, 2025
🌐
LabEx
labex.io › tutorials › java-how-to-nest-ternary-operators-correctly-419963
How to nest ternary operators correctly | LabEx
Master advanced Java ternary operator nesting techniques, improve code readability, and learn best practices for conditional expressions in Java programming.
🌐
SonarSource
rules.sonarsource.com › java › rspec-3358
Java static code analysis: Ternary operators should not be ...
Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your JAVA code
🌐
Linux Hint
linuxhint.com › ternary_operator_java
Java ternary operator – Linux Hint
Nested ternary operator can be used as the alternative of ‘if-else-if’ statement. The use of the nested ternary operator is shown in the following example. Here, an integer value will be taken as input and stored to the marks variable. The value of marks will be checked in the first ternary ...
🌐
SitePoint
sitepoint.com › blog › java › java’s ternary operator in three minutes
Java's Ternary Operator in Three Minutes — SitePoint
November 6, 2024 - Nested conditional operators can hinder readability. int amount = 10; String result = amount <= 2 ? "couple" : amount > 2 && amount < 5 ? "few" : amount <= 5 ? "several" : "many"; Although the example can be improved with formatting, sometimes it’s better to avoid using the ternary operator and go with the plain old if-then-else or switch statement route.
🌐
Reddit
reddit.com › r/react › nested ternary operators. how bad are they?
r/react on Reddit: Nested ternary operators. How bad are they?
January 26, 2024 -

So I saw an article recently that was talking about minimizing the use of ternary operators where possible and reflecting on my own use of them especially in JSX, I have a few questions...

Before I get decided to post my questions, I checked React subs and most discussions on this are a couple years old at least and I thought perhaps views have changed.

Questions:

  1. Is the main issue with using nested ternary operators readability?

I have found myself using ternary operators more and more lately and I even have my own way of formatting them to make them more readable. For example,

            info.type === "playlist"
            ?   info.creationDate
                ?   <div className="lt-info-stats">
                        <span className="text pure">Created on {info.creationDate}</span>
                    </div>
                :   null
            :   info.type === "artist"
                ?   <div className="lt-info-stats">
                        <span className="text pure">{info.genre}</span>
                    </div>
                :   <div className="lt-info-stats">
                        <span className="text pure">{info.releaseDate}</span>
                        <span className="cdot" style={{ fontWeight: "bold", margin: "1px" }}>·</span>
                        <span className="text pure">{info.genre}</span>
                    </div>

When written like this, I can visually see the blocks and tell them apart and it looks a lot like how an if/else might look.

nested ternary operator formatting

2. What is the preferred formatting of ternary operators in general and what do you think should be done to make them more readable?

3. How do people feel about nested ternary operators today? How big of a nono is it to have them in code (if it is a nono)?

I would love you know peoples thoughts on ternary operators in React in general as well.

Thanks for your attention!

🌐
iO Flood
ioflood.com › blog › java-ternary-operator
Ternary Operator in Java: An If...Else Shorthand Guide
February 29, 2024 - If you find yourself nesting multiple ternary operators, consider using traditional if-else statements or switch statements instead. Remember, the main purpose of the ternary operator is to make your code more concise and readable.