There is an operator ? : in Java which can be used to select a value according to the boolean expression. For example, the expression 3 > 2 ? "true" : false will return a string "true". I know we can use if expression to do this, but I will prefer this style because it is concise and elegant.
There is an operator ? : in Java which can be used to select
Share
In Java, there is a difference between
ifand? :and that is thatifis a statement while? :is an expression. In Scala,ifis also an expression: it returns a value that you can for example assign to a variable.The
ifin Scala is much more like? :in Java than theifin Java:You cannot do this in Java:
So, it is really not necessary to have
? :in Scala because it would be exactly the same asif, but with alternative (more obscure) syntax.