• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

Quick question about casting to doubles

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?
  • 2
1 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 :-)

persona = personal

DawkinsAuthor Commented:
ok thanks :)

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now