Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

printing problem

Thread view: 
gk - 14 Sep 2006 18:29 GMT
int a = 5;

            System.out.println("Value is - " + ((a < 5) ? 9.9 : 9));

output: 9.0

where from 9.0 came from ?  there is 9 but NOT 9.0

see, the condition a<5 is false.....so this should print 9

but output is 9.0
Gordon Beaton - 14 Sep 2006 18:50 GMT
> int a = 5;
>
[quoted text clipped - 7 lines]
>
> but output is 9.0

An expression using the conditional operator ?: can only have one type
that must be determined at compile time. Since one of the operands is
float, the other is promoted to float and it becomes a float
expression.

It's described here, and the rule that's applied is called binary
numeric promotion:

http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#290293

/gordon

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

Patricia Shanahan - 15 Sep 2006 18:21 GMT
>> int a = 5;
>>
[quoted text clipped - 19 lines]
>
> /gordon

If this is specifically for printing, could you use strings rather than
numbers?

System.out.println("Value is - " + ((a < 5) ? "9.9" : "9"));

Patricia


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.