• 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:

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

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?
0
Dawkins
Asked:
Dawkins
  • 2
1 Solution
 
TimYatesCommented:
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 :-)

Tim
0
 
TimYatesCommented:
persona = personal

;-)
0
 
DawkinsAuthor Commented:
ok thanks :)
0

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