Jump to content
  • Advertisement
Sign in to follow this  
destructivArts

Java Casting

This topic is 2692 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Could someone please explain Java Casting to me?
The book I'm reading doesn't do a good job of explaining it, and I dont understand why you would want to convert between types of variables.
Actually, could you also explain why you would use a int instead of a long. Why not use double for everything as it can hold the largest values?

Thanks
Peter

Share this post


Link to post
Share on other sites
Advertisement
For starters, ints provide perfect* accuracy (i.e. all int math results in a definite int, except division which just truncates) while floating point operations can start to lose accuracy (you see this more in calculations that have to be repeated a high number of times). Depending on your environment, you may not have the memory space to use all longs or doubles, and ints take up less space.


*perfection being relative, of course.

Share this post


Link to post
Share on other sites
A useful reason for casting.

int x = 5;
int y = 4;

(x / y) is 1
(float)(x / y) is 1.25

Many times you was whole numbers only (binary search for example), it only makes sense that an operation between two ints would also be an int, although you are given the option to cast it otherwise.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!