• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Ternary

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Ternary{
public static void main(String args[])
{
int a = 5;
System.out.println("Value is - " + ((a < 5) ? 9 : 9.3));
}
}

What is the resultant type of ternary operator in this program .Is it int or double???
If ternary result is to be int then why this is not throwing compile error.
As the boolean expression is false.Expression 2 is outputed which is double and double does not fit into int so it must throw compile error.
why this is not happening.
Hope my question is clear.

Thanks
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ramya jp:
public class Ternary{
public static void main(String args[])
{
int a = 5;
System.out.println("Value is - " + ((a < 5) ? 9 : 9.3));
}
}

What is the resultant type of ternary operator in this program .Is it int or double???
If ternary result is to be int then why this is not throwing compile error.
As the boolean expression is false.Expression 2 is outputed which is double and double does not fit into int so it must throw compile error.
why this is not happening.
Hope my question is clear.

Thanks





The boolean expression results to false and hence the output is 9.3. Why do you think there will be problem of assigning double value to int ? I do not see that assignment here.

Morever, if you change the code as shown below.



The output won't be integer 9 it will be 9.0 which is double because the int 9 gets promoted to double. In ternary expresions the promotion always happens to the datatype which is wider.

Hope that helps you
 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Jay
I thought the ternary operator will defaulty take the data type of first expression.

So can i conclude by understanding that the ternary operator will take the highest datatype among exp 2 and exp3.
 
My honeysuckle is blooming this year! Now to fertilize this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic