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 / October 2006

Tip: Looking for answers? Try searching our database.

? : behavior question

Thread view: 
lallous - 17 Oct 2006 22:32 GMT
Hello

I wonder why the output of this:

System.out.println("R=" + ( 4 < 4 ? 1.2 : 2));

Is 2.0 and not 2.

Is Java evaluating the types of both operands and deciding what the returned
type will be?
What is the best place online to read about Java language specifications.

(I come from a C background)

--
Elias
Gordon Beaton - 19 Oct 2006 05:55 GMT
> I wonder why the output of this:
>
> System.out.println("R=" + ( 4 < 4 ? 1.2 : 2));
>
> Is 2.0 and not 2.

Any expression, including one 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#290
293

/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 - 19 Oct 2006 05:57 GMT
> Hello
>
[quoted text clipped - 6 lines]
> Is Java evaluating the types of both operands and deciding what the returned
> type will be?

Correct. See
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.25

> What is the best place online to read about Java language specifications.

The Java Language Specification is on-line at
http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html.

Patricia
Luc The Perverse - 19 Oct 2006 09:00 GMT
> Hello
>
[quoted text clipped - 9 lines]
>
> (I come from a C background)

Even in C, though, you can only have one type in the ternary operator for
the two results.

If you had some type which could not be used to define both, then you would
get a compiler error.

--
LTP

:)
Tim Ward - 19 Oct 2006 10:20 GMT
> I wonder why the output of this:
>
[quoted text clipped - 4 lines]
> Is Java evaluating the types of both operands and deciding what the returned
> type will be?

Used to be called "balancing" back in the days when I wrote compilers. All
(typed) languages have to do something like this.

Signature

Tim Ward
Brett Ward Limited - www.brettward.co.uk



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.