-5

I run a simple example of ternary operator. I wonder why in the second case it doesn't work.

class Gun 
{
    public int hit;
}

public class Test1 {
    public static void main(String[] args) {
        Gun weapon1=new Gun();
        weapon1.hit=54;
        String es1=new String("You killed him!. Grac!");
        String es2=new String("Ops, you were noticed.");

        System.out.println((weapon1.hit>50 ? es1 : es2)); //this works fine

        weapon1.hit>50 ? System.out.println(es1) : System.out.println(es2); // this doesn't

    }
}

After all, the ternary operator is supposed to work similar to if-else statements and this if-else code works just fine:

if (weapon1.hit>50)
    System.out.println(es1);
else
    System.out.println(es2);

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - not a statement at Test1.main(Test1.java:39) That's the error I get if I uncomment the weapon1.hit>50 ? System.out.println(es1) : System.out.println(es2); line.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
parsecer
  • 4,758
  • 13
  • 71
  • 140
  • `System.out.println(weapon1.hit > 50 ? es1 : es2);`. Also, please learn how to format your code (just use an IDE; it does it for you) to preserve our sanity. – bcsb1001 Jun 18 '16 at 22:07
  • 2
    By *"doesn't work"* you mean that it **doesn't compile**? Like the string literal that is split over 3 lines won't compile? Because, you know, *"doesn't work"* is a lousy description of a problem, and if you get an error, you should show us the error, so we know for sure what you are talking about. – Andreas Jun 18 '16 at 22:10
  • @Pshemo So you decided to silently fix one of at least 3 compile errors in that code? – Andreas Jun 18 '16 at 22:19
  • `Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - not a statement at Test1.main(Test1.java:39)` That's the error I get if I uncomment the `weapon1.hit>50 ? System.out.println(es1) : System.out.println(es2);` line. – parsecer Jun 18 '16 at 22:20
  • 1
    @parsecer paste your errors in the question not here – Seek Addo Jun 18 '16 at 22:21
  • @Andreas I tried to limit problems to line with conditional operator (I assumed that printing multi-line string was not related to described problem since OP claims that if-else code works fine). OP still needs to improve his question farther and provide proper error description. – Pshemo Jun 18 '16 at 22:21
  • 1
    @parsecer You get that error because ternary operator is an *expression*, not a *statement*. – Andreas Jun 18 '16 at 22:23
  • @Tunaki Since find of the dup. Man, I couldn't even fathom that this is such a common problem that you can even *find* a dup chain on here. *Sheesh...* Lack of imagination on my part, I guess. – Andreas Jun 18 '16 at 22:26

2 Answers2

1

The conditional is not simply equivalent to an if-else statement.

From the specification for the Conditional Operator ?:

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.

Community
  • 1
  • 1
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
  • You will not even get around to that compile-time error until you fix at least two other errors in that code. – Andreas Jun 18 '16 at 22:15
1

Ternary operator in Java requires expression while System.out.println is a statement.

ConditionalExpression:
    ConditionalOrExpression
    ConditionalOrExpression ? Expression : ConditionalExpression

You can find docs here.

SerCe
  • 5,826
  • 2
  • 32
  • 53