Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WHY would you cast a float to an int?...

Status
Not open for further replies.

KriDan

Instructor
Feb 6, 2001
2
US
I teach beginning Java at a tech school. I understand what casting is - converting a higher data type to a lower data type...my question is practical application...What would be a situation in which you would WANT to lose the decimal places in a float & convert it to an int?, etc. In your jobs, what situations do you typical cast? Or is it something that you don't use very often?
 
Casting a float to an int is not the same thing like up and down casts you describe, because float and int are primitive types, not objects.
Converting float to int, is just a way to round a float number, that's all I think.
Do you see what I mean?
 
yes mr globos is write .

here in java casting is in simple english converting flaot type variables to Integer or Float objects .'

This is called wrapping .

we use bascially this when we have to pass the integer values to a method and it takes only objects as input parameters so inthis case we cast the float to Integer object.

Ex:-

to see whether to strings are equal are not
.equals() takes objects as parameters so we convert char as strings sought
 
Thank you both. I think Globos hit it correctly...the book I use teaches them casting WAY before they ever learn about objects...so the practical application for it is 3 chapters ahead of where they currently are! Thanks alot!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top