No, you cannot do that. Instead try this:

if(bool1 && bool2) voidFunc1();
Answer from frankie liuzzi on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › core java › ternary operator in java
Ternary Operator in Java | Baeldung
September 24, 2025 - In this quick article, we learned about the ternary operator in Java. It isn’t possible to replace every if-else construct with a ternary operator, but it’s a great tool for some cases and makes our code much shorter and more readable.
🌐
GeeksforGeeks
geeksforgeeks.org › java-ternary-operator-with-examples
Java Ternary Operator with Examples | GeeksforGeeks
It’s a one-liner replacement for the if-then-else statement and is used a lot in Java programming. We can use the ternary operator in place of if-else conditions or even switch conditions using nested ternary ...
Published   January 4, 2025
🌐
Programiz
programiz.com › java-programming › ternary-operator
Java Ternary Operator (With Example)
In Java, a ternary operator can be used to replace the if…else statement in certain situations.
🌐
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
🌐
Jenkov
jenkov.com › tutorials › java › ternary-operator.html
Java Ternary Operator
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.
🌐
Alvin Alexander
alvinalexander.com › java › edu › pj › pj010018
The Java ternary operator examples | alvinalexander.com
At its most basic, the ternary operator, also known as the conditional operator, can be used as an alternative to the Java if/then/else syntax, but it goes beyond that, and can even be used on the right hand side of Java statements.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 28069007 › how-to-use-the-ternary-operator-in-java-without-an-else-branch
How to use the ternary operator in Java without an else branch - Stack Overflow
It is a compile-time error for ... that your setter is more likely to have no return type, then no it's not possible to use the ternary operator in this context....
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-ternary-operator
Java Ternary Operator | DigitalOcean
August 4, 2022 - As you can see that we are using java ternary operator to avoid if-then-else and switch case statements. This way we are reducing the number of lines of code in java program.
🌐
W3Schools
w3schools.com › java › java_conditions_shorthand.asp
Java Short Hand If...Else (Ternary Operator)
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... There is also a short-hand if else, which is known as the ternary operator because it consists of three operands.
🌐
Educative
educative.io › answers › ternary-operator-in-java
Ternary operator in Java
The ternary operator is a part of Java’s conditional statements. As the name ternary suggests, it is the only operator in Java consisting of three operands. The ternary operator can be thought of as a simplified version of the if-else statement with a value to be returned.
🌐
Wikipedia
en.wikipedia.org › wiki › Ternary_conditional_operator
Ternary conditional operator - Wikipedia
2 days ago - 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 ...
🌐
ScholarHat
scholarhat.com › home
Ternary Operator in Java With Examples (Detailed Guide)
September 5, 2025 - The choice between them should be based on code readability and the specific context of usage. No, the ternary operator is designed for simple binary conditions. For multiple conditions, it's better to use if-else statements.
Top answer
1 of 8
65

No, you can't. But what's the point of this over an if-else statement? Are you really trying to save 7 characters?

if (name.isChecked()) {
    name.setChecked(true);
} else {
    name.setChecked(false);
}

or if you prefer bad style:

if (name.isChecked()) name.setChecked(true); else name.setChecked(false);

Never mind the fact that you can just do (in this case):

name.setChecked(name.isChecked());

The point of the ternary or "conditional" operator is to introduce conditionals into an expression. In other words, this:

int max = a > b ? a : b;

is meant to be shorthand for this:

int max;
if ( a > b ) {
    max = a;
} else {
    max = b;
}

If there is no value being produced, the conditional operator is not a shortcut.

2 of 8
18

I was wondering if it was possible to do a ternary operation but without returning anything.

No it is not possible:

  1. The 2nd and 3rd operands are required to be non-void expressions; i.e. they must produce some actual value.

    "It is a compile-time error for either the second or the third operand expression to be an invocation of a void method." - JLS 15.25.

  2. A ternary expression is an expression, and cannot be used as a statement.

    "Certain kinds of expressions may be used as statements by following them with semicolons." ... and the ternary expression is not one of those kinds - JLS 14.8.

If you really, really want to use a ternary expression but not use the value of the expression, then the simplest thing is to assign the value to a dummy variable, and add an annotation to suppress the warning about the variable not being used.

But a better idea is to use a plain if statement.

If it's not possible in Java is it possible in other languages, if so which ones apply?

I'm a bit rusty, but I believe that C, C++ and Perl all allow arbitrary expressions to be used in places where their values are not used.

🌐
Hero Vired
herovired.com › learning-hub › blogs › ternary-operator-java
Ternary Operator in Java with Examples | Hero vired
In this function, no variables are being set, or anything is returned by the if/else in this function. You must avoid using a Java ternary operator in this code because it is only used to determine the program’s flow.
🌐
Career Karma
careerkarma.com › blog › java › java ternary operator: a step-by-step guide
Java Ternary Operator: A Step-By-Step Guide | Career Karma
December 1, 2023 - For instance, you could write a ... operator in the Java console. Unlike an “if” statement, the ternary operator does not accept an “else” keyword....
🌐
iO Flood
ioflood.com › blog › java-ternary-operator
Ternary Operator in Java: An If...Else Shorthand Guide
February 29, 2024 - The ternary operator in Java is a concise way to perform conditional operations, defined by three operands: a condition, a value if the condition is true, and a value if the condition is false. Formatted with the syntax, dataType variableName = condition ? value_if_true : value_if_false; It’s main use is as shorthand for an if-else statement:
🌐
SitePoint
sitepoint.com › blog › java › java’s ternary operator in three minutes
Java's Ternary Operator in Three Minutes — SitePoint
November 6, 2024 - While both Java and JavaScript have a ternary operator, they are used in slightly different ways. In Java, the ternary operator is a shorthand for an if-else statement and is used to make the code more concise. On the other hand, JavaScript’s conditional operator is more flexible and can be used in more complex expressions.