sareem sareem - 4 months ago 17
Java Question

JAVA Not a statement with ?: if statement

I'm getting an compilation error "Not a statement" for this line of code:

parallel? stats[3]++ : stats[4]++;

can't understand why?!


Quoting from this:

The following types of expressions can be made into a statement
by terminating the expression with a semicolon (;).

Assignment expressions
Any use of ++ or --
Method invocations
Object creation expressions


In addition to expression statements, there are two other kinds of
statements: declaration statements and control flow statements. 

Obviously, your line of code above doesn't fall into any category mentioned above. Hence, the compiler throw an error. Look at the outermost, not the innermost.