Why is it printing "5.0"?
Isn't it suppose to print "5"? Since I added "(int)" before it?
According to the Java Language Specification §15.25, table 15.25-B:
If the second argument is of type int and the third of double, binary numeric promotion int -> double applies. So, the resulting type of the ternary is 'double'.
voja yes but in here both true and false statement have int... what if I used 5.8? It will print "5"
My goal is :
If a = 5.0 the output I want is "5" NOT "5.0"
If a = 5.8 the output I want is "5.8"
Using thernary conditions...
Is it even possible? LOL
The type of the ternary is determined at compile time. The resulting type chosen will be such that least damage is done.
As long as one argument is a floating point type, the result will be floating point (even if losses occur)