-3

Why does this not compute in Java (v1.8). Seems perfectly logical to me....

boolean banana = true;
(banana == true || false) ? System.out.println("True") : System.out.println("False");

Output message: Error: java: not a statement

Arman Nisch
  • 1,132
  • 11
  • 11
  • you need `String res = (banana == true || false) ? "True" : "False";` , `System.out.println()` returns void, or more appropriately `System.out.println(banana ? "True" : "False");` – Ramanlfc Mar 17 '16 at 10:41
  • I wonder what sense this `|| false` should make ... – Tom Mar 17 '16 at 10:44
  • what have you attempted? – amanuel2 Mar 17 '16 at 10:45
  • 1
    The OP obviously attempted what is cited above. Personally I don't see a problem with the quality of the question. Obviousness ought not to equate to downvotes. And it *would* be logical if `println` was an *expression*. `|| false` is a no-op; that's all. – Bathsheba Mar 17 '16 at 10:46
  • 1
    Reopened. This question is also about the expression `(banana == true || false)`. – Bathsheba Mar 17 '16 at 10:52

7 Answers7

12

The ternary conditional operator must return a value. The second and third operands can't be statements that don't return anything. They must be expressions that return a value.

You could switch it to :

System.out.println(banana ? "True" : "False");

Note that banana == true || false is equivalent to banana == true, which is equivalent to banana as banana itself is a boolean type.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
Eran
  • 387,369
  • 54
  • 702
  • 768
2

How about this?

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

The ternary operator always has to return a value which we're printing.


The other way is only using if-else statement, but it's not pretty.

if(banana)
    System.out.println("true");
else
    System.out.println("false");
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
2

The Java Language Specification §15.25 says:

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

Better try like this:

System.out.println(banana ? "true" : "false");
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
2

What you want is

boolean banana = true;
System.out.println(banana ? "True" : "False");

A ? : operator has to return a value and println is a void method. Not only does it do what you want, it is more concise.

Note

banana == true

is the same as

banana

and

x || false

is the same as

x

Also unless you need to print "True" instead of "true" you can do

System.out.println(banana);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

You are using it incorrectly.

One use of the Java ternary operator (also called the conditional operator) is to assign the minimum (or maximum) value of two variables to a third variable, essentially replacing a Math.min(a,b) or Math.max(a,b) method call. Here's an example that assigns the minimum of two variables, a and b, to a third variable named minVal is:

minVal = (a < b) ? a : b;

You can do it like this.

if(boolean)
   System.out.println("True");
else  
   System.out.println("False");
Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
0

I guess because Java won't allow statement like that.

Try using if statement.

boolean banana = true;
if (banana == true || false) System.out.println("True"); else System.out.println("False");
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
0

Because there are only false bananas: https://en.wikipedia.org/wiki/False_banana. There is not any one "true" banana. You are likely thinking of the "true plantain", see https://en.wikipedia.org/wiki/True_plantains. Changing your banana to false will allow your code to once again be copacetic with biology.

Alex Meiburg
  • 655
  • 5
  • 20