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....
🌐
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
Discussions

java - Ternary Operator - Stack Overflow
An if statement in Java does not have a return type; to recode it using a ternary operator then you would have to add one. More on stackoverflow.com
🌐 stackoverflow.com
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 16, 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 13, 2020
🌐
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 ...
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.

🌐
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.
🌐
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.
Find elsewhere
🌐
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..
🌐
Wikipedia
en.wikipedia.org › wiki › Ternary_conditional_operator
Ternary conditional operator - Wikipedia
January 27, 2026 - In computer programming, the ternary conditional operator is a ternary operator that evaluates to one of two values based on a Boolean expression. The operator is also known as conditional operator, ternary if, immediate if, or inline if (iif). Although many ternary operators are theoretically ...
🌐
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 ...
🌐
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.
🌐
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 ?
🌐
Hero Vired
herovired.com › learning-hub › blogs › ternary-operator-java
Ternary Operator in Java with Examples | Hero vired
March 19, 2024 - But new developers often find difficulty in understanding the syntax and symbols of the Java ternary operator. ... Master cloud architecture, DevOps practices, and automation to build scalable, resilient systems. ... Only the Java ternary operator supports three operands in a conditional statement.
🌐
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....
🌐
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
🌐
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.
🌐
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 16, 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.

🌐
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.
🌐
Medium
medium.com › @whitejsx › ternary-operator-in-java-f77480380f63
Ternary operator in Java. the ternary operator is more readable… | by White christopher | Medium
June 5, 2023 - This ternary operator consists of three operands: the value of the expression num % 2 == 0, and two string literals "even" and "odd". Its result type is String. Note: Java allows us to nest one ternary operator into another one, but it can be ...
🌐
SitePoint
sitepoint.com › blog › java › java’s ternary operator in three minutes
Java's Ternary Operator in Three Minutes — SitePoint
November 6, 2024 - The ternary operator is a form of syntactic sugar for if-then-else statements. It is also known as the conditional operator, which is perhaps a more meaningful name because it evaluates conditions like if does.