Ternary operator

前端 未结 5 1652
野性不改
野性不改 2020-12-19 08:44

Why the output of following code is 9.0 and not 9 ? If ternary operator is nothing but short form of if-else branch then why java compiler is promoting int to double ?

5条回答
  •  無奈伤痛
    2020-12-19 09:09

    Java needs to know the type of the result at compile time. So as this ternary operator can result an int or a double, the compiler chooses the double as the result type.

提交回复
热议问题