Well, the ternary operator in Java acts like this...

return_value = (true-false condition) ? (if true expression) : (if false expression);

...Another way of looking at it...

return_value = (true-false condition) 
             ? (if true expression) 
             : (if false expression);

You question is kind of vague and we have to assume here.

  • If (and only if) callFunction(...) declares a non-void return value (Object, String, int, double, etc..) - it seems like it does not do that via your code - then you could do this...

    return_value = (string != null) 
                 ? (callFunction(...)) 
                 : (null);
    
  • If callFunction(...) does not return a value, then you cannot use the ternary operator! Simple as that. You will be using something that you don't need.

    • Please post more code to clear up any issues

Nonetheless, ternary operator should represent alternative assignments only!! Your code does not seem to do that, so you should not be doing that.

This is how they should work...

if (obj != null) {            // If-else statement

    retVal = obj.getValue();  // One alternative assignment for retVal

} else {

    retVal = "";              // Second alternative assignment for retVale

}

This can be converted to...

retVal = (obj != null)
       ? (obj.getValue())
       : ("");

Since it seems like you might be trying to just refactor this code to be a one-liner, I have added the following

Also, if your false-clause is truely empty, then you can do this...

if (string != null) {

    callFunction(...);

} // Take note that there is not false clause because it isn't needed

OR

if (string != null) callFunction(...);  // One-liner
Answer from Christopher Rucinski on Stack Overflow
🌐
W3Schools
w3schools.com › java › java_conditions_shorthand.asp
Java Short Hand If...Else (Ternary Operator)
Java Examples Java Videos Java ... is also a short-hand if else, which is known as the ternary operator because it consists of three operands....
Top answer
1 of 4
67

Well, the ternary operator in Java acts like this...

return_value = (true-false condition) ? (if true expression) : (if false expression);

...Another way of looking at it...

return_value = (true-false condition) 
             ? (if true expression) 
             : (if false expression);

You question is kind of vague and we have to assume here.

  • If (and only if) callFunction(...) declares a non-void return value (Object, String, int, double, etc..) - it seems like it does not do that via your code - then you could do this...

    return_value = (string != null) 
                 ? (callFunction(...)) 
                 : (null);
    
  • If callFunction(...) does not return a value, then you cannot use the ternary operator! Simple as that. You will be using something that you don't need.

    • Please post more code to clear up any issues

Nonetheless, ternary operator should represent alternative assignments only!! Your code does not seem to do that, so you should not be doing that.

This is how they should work...

if (obj != null) {            // If-else statement

    retVal = obj.getValue();  // One alternative assignment for retVal

} else {

    retVal = "";              // Second alternative assignment for retVale

}

This can be converted to...

retVal = (obj != null)
       ? (obj.getValue())
       : ("");

Since it seems like you might be trying to just refactor this code to be a one-liner, I have added the following

Also, if your false-clause is truely empty, then you can do this...

if (string != null) {

    callFunction(...);

} // Take note that there is not false clause because it isn't needed

OR

if (string != null) callFunction(...);  // One-liner
2 of 4
3

Yes. You can with keeping same null in else block.

String result = str !=null ?  callFunction(parameters) : null;

Make sure that callFunction(parameters) return a String.

Discussions

[Java] Please can someone explain the Ternary Operator
cond?if_val:else_val If cond is true, then the value is if_val, otherwise it is else_val. That's it. More on reddit.com
🌐 r/learnprogramming
17
0
July 20, 2015
What is so awful about the ternary operator?

Depends on how it's used, it should improve readability if used correctly. It is however often misused and that might be why Sonar doesn't like it. The rules should be configured to fit the needs of the project, if the review process ensures that the operator is used in appropriate situations just change the rule.

More on reddit.com
🌐 r/java
93
76
October 14, 2017
Is using the ternary operator bad practice?
If you don't abuse the ternary operator as if replacement and if there is no nesting, it is perfectly fine to use the ternary operator. Actually, it is far more elegant to write int y = (x > 0) ? x : x * (-1); Than int y; if (x > 0) { y = x; } else { y = x * (-1); } Nested ternary operators are a no-go. More on reddit.com
🌐 r/learnprogramming
11
1
February 2, 2022
Ternary Operators

I use them frequently. If it's a simple case like:

if (someValue > 0) {
    label = "positive";
}
else {
    label = "negative";
}

Then I'll essentially always replace that with:

label = (someValue > 0) ? "positive" : "negative";

I'll also often replace:

if (someValue > 0) {
    doSomething("positive");
}
else {
    doSomething("negative");
}

With:

doSomething(someValue > 0 ? "positive" : "negative");

Reasons not to use a ternary might include:

  • I need to do something (like logging, or calling some other function), rather than just assigning a value

  • The line ends up being long / complex / messy and it's more readable to break it up

  • I'd end up nesting ternaries (usually that gets ugly)

More on reddit.com
🌐 r/learnjava
12
22
July 11, 2020
🌐
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
🌐
Nimble Technocrats
nimbletechnocrats.com › home › ternary operator in java with example explained
Ternary Operator in Java with Example Explained
October 4, 2025 - In this example, the program declares class Ternary and assigns n1=6, n2=10. In this program, the ternary operator assesses if n1 is greater than n2. If the n1 is greater than n2, then the value of n1 will be shown. Otherwise, the value of n2 will be displayed. In Java, you can use the ternary operator to replace if-else statements.
🌐
Baeldung
baeldung.com › home › java › core java › ternary operator in java
Ternary Operator in Java | Baeldung
September 24, 2025 - The ternary conditional operator ?: allows us to define expressions in Java.
Find elsewhere
🌐
Wikipedia
en.wikipedia.org › wiki › Ternary_conditional_operator
Ternary conditional operator - Wikipedia
2 days ago - A true ternary operator exists for arithmetic expressions: ((result = condition ? value_if_true : value_if_false)) ... Where condition can be any bash command. When it exits with success, the first echo command is executed, otherwise the second one is executed. The following code in C assigns result to the value of x if a > b, and otherwise to the value of y. This is the same syntax as in many related languages including C++, Java...
🌐
Coderanch
coderanch.com › t › 711155 › java › Ternary-operator
Ternary operator (Beginning Java forum at Coderanch)
June 13, 2019 - "Ternary" is just a description for the type of operator. Monadic operators have only one operand: i++ Binary operators have 2 operands: a + b Ternary operators have 3 operands: (a < b)? a+1 : b-1 You can use the name "ternary" because there's only one ternary operator in Java, but it's a sloppy ...
🌐
DEV Community
dev.to › luthfisauqi17 › enhance-your-java-code-effectiveness-using-ternary-operator-2h3b
Enhance Your Java Code Effectiveness Using Ternary Operator - DEV Community
October 16, 2022 - My personal opinion is that the ternary operator is very similar to the concept of Value increment, in that you can shorten var = var + 1 to var++. So if the concept of value increment can be used to shorten the increment value operation, the ternary operator can be used to shorten the coding of conditional statements.
🌐
Igmguru
igmguru.com › blog
Blog
Globally, companies use Java for building applications and websites for their users. Understanding this programming language means acing one of the prerequisites that qualifies a career in app or software development. This blog discusses..
🌐
Mkyong
mkyong.com › home › java › java ternary operator examples
Java Ternary Operator examples - Mkyong.com
October 16, 2019 - package com.mkyong.test; public class JavaExample1 { public static void main(String[] args) { int age = 10; String result = ""; if (age > 18) { result = "Yes, you can vote!"; } else { result = "No, you can't vote!"; } System.out.println(result); } } ... No, you can't vote! 1.2 With ternary operator, code can be simplified like this:
🌐
JanBask Training
janbasktraining.com › community › java › ternary-operator
Ternary Operator | JanBask Training Community
August 16, 2025 - The ternary operator is a shorthand way to write simple conditional statements in many programming languages like Java, JavaScript, Python (with a variation), and C. It’s called ternary because it takes three operands: a condition, a value ...
🌐
BeginnersBook
beginnersbook.com › 2022 › 09 › ternary-operator-in-java-with-examples
Ternary Operator in Java with Examples
A ternary operator starts with a condition followed by a question mark (?), then an expression to execute if the condition is ‘true; followed by a colon (:), and finally the expression to execute if the condition is ‘false’. This operator is frequently used as a one line replacement for if…else statement. ... If condition is true, the Expression1 executes. If condition is false, the Expression2 executes. ... class JavaExample { public static void main(String[] args) { int age = 16; boolean isAdult = age >= 18 ?
🌐
Jenkov
jenkov.com › tutorials › java › ternary-operator.html
Java Ternary Operator
May 12, 2024 - The Java ternary operator works like a simplified if-statement which returns one of two possible values, depending on a given condition. This Java ternary operator tutorial explains how to use the ternary operator in Java.
🌐
Java Development Journal
javadevjournal.com › home › ternary operator in java
Ternary Operator in Java | Java Development Journal
September 13, 2024 - The ternary operator in Java is a shorthand way of writing simple if-else statements. It allows developers to condense complex statements into a single line of code, making the code more readable and concise.
🌐
Reddit
reddit.com › r/java › what is so awful about the ternary operator?
r/java on Reddit: What is so awful about the ternary operator?
October 14, 2017 -

SonarQube flags this a a major code smell. It seems to be OK to use it with a more functional language like JavaScript, but for some reason should be avoided at all costs when coding in Java.

I think one of the worst things about Java is its verbosity. I find that the newer, more functional extensions help with that a lot, and the ternary operator (although it's been around much longer) works very well with that programming style.

If it's such a bad thing, why was it made part of the language?

Top answer
1 of 25
101

There's nothing awful about it. It might be just easy to abuse.

I think it should be used for simple expressions that fit on one line, and not to evalute complex expressions.

Good:

String x = datehour >= 12 ? "PM" : "AM";

Bad:

String x = foo.getBaz().getBar(a, b * 13, 42) != null && x.doSomething() < y.some().other().long().expression() ? foo.calculate().something().ifTrue(123) : array[bar.getIndex()].stream().map(x -> x.getField()).collect(Collectors.toList());

I believe that unreadable code is never the fault of a language construct (ternary operator in this case) but the programmer who wrote it. Many of the syntaxes can be abused and result in hard to read code. It's fine to discourage its use to help increasing readability in larger operations, but saying it should be avoided at all costs is ridiculous.

major code smell

https://en.wikipedia.org/wiki/Code_smell

a code smell is any characteristic in the source code of a program that possibly indicates a deeper problem.

It's definitely not a code smell in itself.

I think one of the worst things about Java is its verbosity.

I think on the contrary (opinion). I believe it is a major benefit that Java is verbose, and doesn't have simple syntax for expressing complex operations. It makes it easier to read, as readers doesn't have to keep in mind too may contextual information when reading a given expression. It also makes newcomers understand the code easier without requiring them to google for every second syntax.

Following up, I also think that it is for the better that Java doesn't have properties like C#, the .? operator, operator overloading, and others.

2 of 25
53

Depends on how it's used, it should improve readability if used correctly. It is however often misused and that might be why Sonar doesn't like it. The rules should be configured to fit the needs of the project, if the review process ensures that the operator is used in appropriate situations just change the rule.

🌐
SoftwareTestingo
softwaretestingo.com › home › java › java tutorial › ternary operator in java
Ternary Operator In Java Example With Easy Explanation 2026
January 5, 2024 - In this post, we will discuss one ... similar to the Java If Statement. This ternary operator validates a condition; after evaluating it, it returns true or false....
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-ternary-operator
Java Ternary Operator | DigitalOcean
August 4, 2022 - Java ternary operator is the only conditional operator that takes three operands. Java ternary operator is a one liner replacement for if-then-else statement and used a lot in java programming.
🌐
Quora
quora.com › What-is-the-syntax-of-ternary-operator-in-Java
What is the syntax of ternary operator in Java? - Quora
Java ternary operator is a one liner replacement for if-then-else statement and used a lot in java programming. The first operand in java ternary operator should be a boolean or a statement with bo...
🌐
Codespindle
codespindle.com › Java › Java_Ternary.html
Ternary Operator and Nested Ternary Operators in Java
int number = 10; String result = (number % 2 == 0) ? "Even" : "Odd"; System.out.println(result); // Prints "Even" The ternary operator can also be used to assign values to variables. The following example shows how to use the ternary operator to assign the value "true" to the variable isAdult if the user's age is greater than or equal to 18, and the value "false" otherwise: Java