By TylerKinkade

2012-02-26 05:39:37 8 Comments

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

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

name.isChecked() ? name.setChecked(true):name.setChecked(false);


@Amir Pashazadeh 2012-02-26 06:01:53

Sometimes, you can use ternary operation on method arguments to solve your request.

name.setChecked(name.isChecked() ? true : false);

By the way, the best solution for your problem is


@Sam 2012-02-26 05:48:46

In java following code isn't possible:

(your-condition) ? (true-statements) : (false-statements)

for sample you can't compile following snipet code :

(1==1) ? System.out.println("") : System.out.println("");

you achieve following compilation error:

The left-hand side of an assignment must be a variable

@jalv1039 2016-10-16 11:49:54

(your-condition) ? (true-statements) : (false-statements) is possible in java but it has to return a value (it cannot be void) and you have to assign it to a variable.

@Android 2016-12-20 05:21:49

You have to return some value and it will not work if you want it to act like a void method which performs some action without a returning a value.

Hope this helps...

@zuazo 2016-12-20 05:41:48

This question is already answered. Please, read the FAQ to learn how to ask questions properly.

@Stephen C 2012-02-26 06:12:07

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.

@Mark Peters 2012-02-26 05:42:11

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()) {
} else {

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):


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.

Related Questions

Sponsored Content

66 Answered Questions

[SOLVED] How do I generate random integers within a specific range in Java?

  • 2008-12-12 18:20:57
  • user42155
  • 4056947 View
  • 3502 Score
  • 66 Answer
  • Tags:   java random integer

49 Answered Questions

[SOLVED] Does a finally block always get executed in Java?

44 Answered Questions

[SOLVED] How do I convert a String to an int in Java?

87 Answered Questions

[SOLVED] Is Java "pass-by-reference" or "pass-by-value"?

32 Answered Questions

[SOLVED] When to use LinkedList over ArrayList in Java?

57 Answered Questions

[SOLVED] How to create a memory leak in Java?

21 Answered Questions

[SOLVED] How do I call one constructor from another in Java?

  • 2008-11-12 20:10:19
  • ashokgelal
  • 863792 View
  • 2249 Score
  • 21 Answer
  • Tags:   java constructor

23 Answered Questions

[SOLVED] How do I check if a file exists in Java?

59 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

Sponsored Content