20

I am trying to use ? to decide which method i want to call, but i do not need to assign a variable. My question: Is there a way to use the ternary operator with out assigning a variable?

(something i dont need) = (x == 1)? doThisMethod():doThatMethod()

instead of

if(x == 1) {
    doThisMethod()
} else {
    doThatMethod()
}
Abdull
  • 26,371
  • 26
  • 130
  • 172
Trae Moore
  • 1,759
  • 3
  • 17
  • 32
  • Have you simply tried it with `void` methods and no assignment target? (I never have, but it's not clear that it wouldn't work.) – Hot Licks Sep 28 '12 at 16:37
  • 1
    sorry my answer was wrong: you _can't_ use the ternary operator as a lone statement in java; you _can_ in C. – pb2q Sep 28 '12 at 16:41

4 Answers4

29

This will not work, as it is not the intended use of the ternary operator.

If you really want it to be 1 line, you can write:

if (x==1) doThisMethod(); else doThatMethod();
ruakh
  • 175,680
  • 26
  • 273
  • 307
Kevin DiTraglia
  • 25,746
  • 19
  • 92
  • 138
  • This worked. and thank you for the knowledge. I do want to keep it easy for other people to follow. – Trae Moore Sep 28 '12 at 16:45
  • 1
    -1: completely wrong. This has nothing to do with the return-type of the method. Changing the methods to return (say) `int` or `Object` would not fix the syntax error. – ruakh Sep 28 '12 at 16:47
  • @ruakh, the question that i asked is basically can i make the ternary operator basically a void so that i wouldnt have to assign a variable. for the question that i aske this is the best answer. (because, i actually didnt need the ternary operator, just an "else". so if anybody gets the -1 its me. – Trae Moore Sep 28 '12 at 16:52
  • @ruakh it works fine if the methods have a return type (As I stated in the answer), I just tried it. – Kevin DiTraglia Sep 28 '12 at 16:57
  • @KDiTraglia: No, you misunderstood the question. The question asked, "Is there a way to use the ternary operator with out assigning a variable?". In essence, your answer says: "Yes, it's possible to use the ternary operator without assigning a variable. All you have to do is -- assign a variable." – ruakh Sep 28 '12 at 17:04
  • and i did actually end up returning something and this did compile. – Trae Moore Sep 28 '12 at 17:05
  • I've now edited the answer to remove all the wrong claims, and will therefore retract my down-vote. – ruakh Sep 28 '12 at 17:06
6

I doubt that this works. The JLS §15.25 defines the ternary expression as follows:

ConditionalExpression:
    ConditionalOrExpression
    ConditionalOrExpression ? Expression : ConditionalExpression

And a ConditionalExpression isn't a Statement by itself. It can be used in various other places, though, e.g. an Assignment:

AssignmentExpression:
    ConditionalExpression
    Assignment

Assignment:
    LeftHandSide AssignmentOperator AssignmentExpression
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
1

According to §14.8 "Expression Statements" of the Java Language Specification, the only expressions that can be used alone as statements are:

  • assignments
  • pre– and post-increments and pre– and post-decrements
  • method calls
  • class instance creation expressions (constructor calls)
ruakh
  • 175,680
  • 26
  • 273
  • 307
-5

Much more diversified, if the flow was to break after the method call.this can be used, but a word of caution if the flow does not break after the if then both methods will get executed.

if (x==1) 
doThisMethod();
doThatMethod();
Aniruddha K.M
  • 7,361
  • 3
  • 43
  • 52
  • This will calll `doThatMethod();` all the time, no matter what x is. – hinneLinks Jan 08 '16 at 06:15
  • @hinneLinks made it little bit more verbose, sorry for the confusion – Aniruddha K.M Jan 08 '16 at 06:19
  • If you mean `return doThisMethod();` then you should write it. If you mean, that you rely on `doThisMethod()` to throw an Exception in order to prevent the call of `doThatMethod();` this would be very bad code style, nobody would expect that. – hinneLinks Jan 08 '16 at 06:28