Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Shortcut if-else  RSS feed

 
Greg Neef
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given that v.inStock() returns a boolean, why does this compile?

but this does not

[greg@localhost servlet]$ javac SomeProgram.java
SomeProgram.java:32: not a statement
(v.inStock()) ? out.print( "<td> yes </td>" ) : out.print( "<td> no </td>" );
 
Mike Minner
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In the ?: ternary operator, the first operand must be a boolean expression, which you clearly have. The second and third operands must be expressions. You do not have expressions, you have statements. It should work this way:

Mike
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!