wood burning stoves 2.0*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ternary query
 
Similar Threads
Simple JQuest qn
Primitive data types
Jxam question
looping in java
Java loops- linear/log time- calculation