9

I have the following code:

public class boolq {
    public static void main(String[] args) {
        boolean isTrue = true;
        isTrue ? System.out.println("true"): System.out.println("false");       
    }
}

when I try to compile it i get this:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:

Syntax error on token ";", assert expected after this token

Type mismatch: cannot convert from void to boolean

at boolq.main(boolq.java:3)

what am I doing wrong?

java -version

java version "1.6.0_15"

Java(TM) SE Runtime Environment (build 1.6.0_15-b03)

Java HotSpot(TM) Client VM (build 14.1-b02, mixed mode, sharing)

0

5 Answers 5

22

The ternary operator is an expression, and evaluates to one of the two values that you pass to it.

Since System.out.println doesn't return a value, you can't put it inside the ternary operator.

You need to write System.out.println(isTrue ? "true" : "false");

6
  • 1
    Common mistake, it's the Ternary operation - en.wikipedia.org/wiki/Ternary_operation
    – jball
    Jan 6, 2010 at 0:54
  • @jball: You're right; tertiary means third-order.
    – SLaks
    Jan 6, 2010 at 0:55
  • this is why languages shouldn't distinguish between expressions and statements!
    – Claudiu
    Jan 6, 2010 at 1:30
  • Common mistake, it's a ternary operator, it's the conditional operator. It just happens to be the only ternary (three operands) operator. A bit pedantic perhaps, so feel free to ignore me, but it's good to know these things IMO.
    – Blorgbeard
    Jan 6, 2010 at 2:50
  • 1
    I disagree. Until someone makes a second ternary operator, it's also the ternary operator.
    – SLaks
    Jan 6, 2010 at 3:04
9

There are actually two errors in this statement.

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

As @Slaks points out, the ternary '?' operator requires that the second and third operand expressions have a non-void type. That explains the message

"Type mismatch: cannot convert from void to boolean".

But the message

"Syntax error on token ";", assert expected after this token"

is saying something different!! This happens because not all Expressions can be used as Statements in Java. In short, this would still be an error, even after you replaced the println calls with non-void expressions.

The applicable part of Java grammar says this:

ExpressionStatement:
    StatementExpression ;

StatementExpression:
    Assignment
    PreIncrementExpression
    PreDecrementExpression
    PostIncrementExpression
    PostDecrementExpression
    MethodInvocation
    ClassInstanceCreationExpression

Notice that the ternary expression is not in the list. The parser is then assuming (incorrectly) that you were trying to write an AssertStatement, and saying that you left out the assert keyword at the start of the line.

4

haven't used Java in a while, but I would use this syntax to solve the problem:

public class boolq {
    public static void main(String[] args) {
        boolean isTrue = true;
        System.out.println(isTrue ? "true" : "false");       
    }
}
3

You're trying to use an expression in a statement context. Try:

public class boolq { 
    public static void main(String[] args) { 
        boolean isTrue = true; 
        if (isTrue) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    } 
}
1

If you still want to do it using "short if", so try this:

boolean isTrue;     
PrintStream ps = isTrue ? System.out : System.err;
ps.println(String.valueOf(isTrue));

Not the answer you're looking for? Browse other questions tagged or ask your own question.