File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

ternary query

Vishal Mungi

Joined: Mar 24, 2005
Posts: 24
class f
public static void main(String[] args)
int x=6;
double d=7.7;

why does this print 9.0 & not 9 ???
Vishal Mungi

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.


Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: ternary query
It's not a secret anymore!