3

If you write something like:

    boolean condition;
    (...)
    String out = condition ? "true" : "false";
    System.out.println(out);

It works. But if you write

    condition ? System.out.println("true") : System.out.println("false");

you get a "not a statement" error. The "correct" way is to write (the usage of braces or "to be or not to be in one line" is out of the scope of the question):

    if (condition)
        System.out.println("true");
    else
        System.out.println("false");

Why? The one line ifs must always return a value?

EDIT: To everyone pointing out that

    condition ? System.out.println("true") : System.out.println("false");

is not a correct syntax, yeah I got that part. I am not asking for solutions (although the

    System.out.println(condition ? "true" : "false");

is nice.

@Andrew Tobilko where is that stated? THAT is what I'm interested in.

EDIT2: The accepted answer provides exactly what I wanted. Thanks

Camandros
  • 449
  • 6
  • 22
  • PS: your question seems to be changing from "why does it not work" to "why doesn't java support this feature that I just came up with". You're doing a lot of EDITs in my opinion. In all honesty, your question is no longer a question. – bvdb Mar 23 '16 at 12:16

4 Answers4

7

condition ? System.out.println("true") : System.out.println("false"); is not a statement.

From here:

In computer science, a ternary operator is an operator that takes three arguments.

System.out.println("true") does not qualify to be an argument, as the method println() is of void type. Hence, it is not a statement.

Use this instead:

System.out.println(condition ? "true" : "false");

dryairship
  • 6,022
  • 4
  • 28
  • 54
  • In all honesty, this is by far the worst answer. What's the deal with the "three arguments", his number of arguments is correct, isn't it ? – bvdb Mar 23 '16 at 12:20
  • 1
    @bvdb **No.**. As I have said, _System.out.println("true") does not qualify to be an argument, as the method println() is of void type._ – dryairship Mar 23 '16 at 12:28
2

Your case:

System.out.println() doesn't return any value (returns void). The ternary operator expects that its two parts return statements.

System.out.println(condition ? "true" : "false");
System.out.println(condition); // it's an equalent to the previous line

Some theory from the specification:

ConditionalExpression:
    ConditionalOrExpression
    ConditionalOrExpression ? Expression : ConditionalExpression
  • The conditional operator is syntactically right-associative (it groups right-to-left). Thus, a?b:c?d:e?f:g means the same as a?b:(c?d:(e?f:g)).
  • The conditional operator has three operand expressions. ? appears between the first and second expressions, and : appears between the second and third expressions.
  • The first expression must be of type boolean or Boolean, or a compile-time error occurs.
  • It is a compile-time error for either the second or the third operand expression to be an invocation of a void method.

More information is here.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
0

This is not correct syntax(Your code):

 condition ? System.out.println("true") : System.out.println("false");

This acomplishes the similar goal but has correct syntax

System.out.println(Condition ? "true"  :  "false")

System.out.println() is not an argument.. Thats just for printing. To actually print you need to have an actual string! "example" . And dont forget ternary operator takes three arguments.. The condition your evaluating, the 1st result if return true, and if return false.Hope this helped!

Syntax:

ConditionToBeEvaluated ? IfTrue : IfFalse
amanuel2
  • 4,508
  • 4
  • 36
  • 67
0

The ternary operator should be used as :

field = condition ? valueIfTrue : valueIfFalse;

Yes, the 2 possible options, each need to have a value (which is not the case if you call a void method such as System.out.println(...)).

In your case you could write:

System.out.println(condition? "true" : "false");
bvdb
  • 22,839
  • 10
  • 110
  • 123