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

about ternary operator  RSS feed

 
shankar reddy
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int i=5;

System.out.println(((i>5)?9.9:9));

can anyone clear my doubt why the answer is 9.0 ?
 
Ganesh Kumar
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi shankar,

The condition expression returns the value according to the first one that is in this case 9.9 .If you change it to some other data type then the return will also be according to it.So the return is in the format of the first one


Just try this



Now you can understand i think!!!
 
shankar reddy
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for reply.

say
it i =5;
if(i>5)
{
System.out.println("inside if");
}
else
{
System.out.println("inside if");
}

in above scenario is there any chance of entering into the if block.
not at all.

so how it is taking floating value instead of int .

could you clarify it again.
 
Ganesh Kumar
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No it wont.In ternary operator it changes because you did not say the return type.Here you specified as int so it value wont get changed
 
shankar reddy
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry Ganesh Kumar...
i am not getting your answer
 
shankar reddy
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry Ganesh Kumar...
i am not getting your answer
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ganesh kumar :
If you change it to some other data type then the return will also be according to it.So the return is in the format of the first one


As Ganesh kumar told and as per the JLS, the "Binary Numeric Promotion" is applied, and thats why you get the floating point value.

The JLS in the above link says,


When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value of a numeric type, the following rules apply, in order, using widening conversion (�5.1.2) to convert operands as necessary:

  • If either operand is of type double, the other is converted to double.
  • Otherwise, if either operand is of type float, the other is converted to float.
  • Otherwise, if either operand is of type long, the other is converted to long.
  • Otherwise, both operands are converted to type int.


  • The original link to the topic is here for the Conditional Operators in JLS.

    HtH.
    [ July 09, 2007: Message edited by: Raghavan Muthu ]
     
    Jeremy Botha
    Ranch Hand
    Posts: 125
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi, Shankar.

    To simplify what others have listed here, your ternary expression is of the format:

    integer expression ? double value : integer value

    Since one of the result values is a double, the other will be treated as a double. If your expression was of the format

    int i = 5;
    if(i > 5) ? 10 : 9;

    Then you would see 9 printed out.

    J
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!