sareem sareem - 1 year ago 132
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?!

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download