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

Tip: Looking for answers? Try searching our database.

internal int to double cast

Thread view: 
Bart Rider - 10 May 2006 12:59 GMT
Hi there,

I just want to have a confirmation. I have two
variables like this:
    int i = 10;
    double d = 10.1;

Now I test whether d is greater and do such a thing:
    if (i<d)
        greater_double();
    else
        smaller_or_equal_double();

My question is the following. Does Java internally
converts the int(eger) i to double before comparison
(as I assume it does) or might there be circumstances
where it did not in such a case?

Best regards,
Bart
Hendrik Maryns - 10 May 2006 13:27 GMT
Bart Rider schreef:
> Hi there,
>
[quoted text clipped - 13 lines]
> (as I assume it does) or might there be circumstances
> where it did not in such a case?

It does.  See
http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#26917

HTH, H.

- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
liuyanbupt@gmail.com - 10 May 2006 13:34 GMT
I find the following statements in "The Java™ Language Specification
Third Edition":
-----------------------------------------------------------------------------------------------------------------
5.6.2 Binary Numeric Promotion
...
· If either operand is of type double, the other is converted to
double.
...

15.20.1 Numerical Comparison Operators <, <=, >, and >=
The type of each of the operands of a numerical comparison operator
must be a
type that is convertible (§5.1.8) to a primitive numeric type, or a
compile-time
error occurs. Binary numeric promotion is performed on the operands
(§5.6.2). If
the promoted type of the operands is int or long, then signed integer
comparison
is performed; if this promoted type is float or double, then
floating-point comparison
is performed.
-----------------------------------------------------------------------------------------------------------------
So, I think what you assumed is the right answer.
Thomas Fritsch - 10 May 2006 13:40 GMT
Bart Rider schrieb:
> I just want to have a confirmation. I have two
> variables like this:
[quoted text clipped - 11 lines]
> (as I assume it does) or might there be circumstances
> where it did not in such a case?

Just one addition to what the other posters correctly said:

You can check it by disassembling your java class with "javap -c ...".
There should be an "i2d" (int to double) instruction.

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

Patricia Shanahan - 10 May 2006 14:01 GMT
> Hi there,
>
[quoted text clipped - 16 lines]
> Best regards,
> Bart

The JLS requires the same answer as if the program did the conversion of
i to double followed by the comparison.

An optimizing compiler might not actually generate the conversion, if it
noticed that i<d is a constant expression, did the comparison at compile
time, found that is is always true, and collapsed the if statement.

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.