*
The moose likes Beginning Java and the fly likes ternary query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ternary query" Watch "ternary query" New topic
Author

ternary query

Vishal Mungi
Greenhorn

Joined: Mar 24, 2005
Posts: 24
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

Joined: Mar 24, 2005
Posts: 24
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ternary query