Quick question about casting to doubles

Posted on 2004-11-12
Last Modified: 2010-03-31
Hi all quick Java newbie question,

With a calculation like this:

int ten = 10;
int three = 3;
double out = ten / three;

out will equal 3.  But if I cast *either* ten or three to a double , I get the correct result of 3.333333.

What is the correct thing to do?  Cast both to double(which seems logical) or only one(which minimises the amount of casting)?  

Or shouldn't I be getting into this situation in the first place?
Question by:Dawkins
    LVL 35

    Accepted Solution

    I always cast both...

    it doesn't matter

    if you cast one, then it automagically casts the other one (to make it up to the same bit length as the larger one)

    doing both yourself, just makes it a bit more obvious...

    I guess it's down to persona preference :-)

    LVL 35

    Expert Comment

    persona = personal


    Author Comment

    ok thanks :)

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    The viewer will learn how to implement Singleton Design Pattern in Java.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now