tags:

views:

212

answers:

4

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)

+3  A: 

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");
        }
    } 
}
Greg Hewgill
+3  A: 

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");       
    }
}
Marek Karbarz
+12  A: 

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");

SLaks
Common mistake, it's the Ternary operation - http://en.wikipedia.org/wiki/Ternary_operation
jball
works like a charm
Midday
@jball: You're right; tertiary means third-order.
SLaks
this is why languages shouldn't distinguish between expressions and statements!
Claudiu
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
I disagree. Until someone makes a second ternary operator, it's also _the_ ternary operator.
SLaks
+2  A: 

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.

Stephen C