• Post Reply Bookmark Topic Watch Topic
  • New Topic

ternary query

 
Vishal Mungi
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class f
{
public static void main(String[] args)
{
int x=6;
double d=7.7;
System.out.println((x>d)?99.9:9);
}
}


why does this print 9.0 & not 9 ???
 
Vishal Mungi
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh yes , i got it ..
its the same as a method returning one of two values & it
just converts both to the same datatype, in this case the 'bigger of the two'.
[ October 02, 2005: Message edited by: Vishal Mungi ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That seems like the logical answer without looking at the JLS itself. It seems that the ternary operator promotes its operands just like other operators do. Since 99.9 is a double and 9 is an int, the later is promoted to a double.

Layne
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!