This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes int to float conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "int to float conversion " Watch "int to float conversion " New topic

int to float conversion

ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198


i have an integer and i cant understand why i can assign its value to a float without casting ?
Ankit Garg

Joined: Aug 03, 2008
Posts: 9452

Are you trying to convert Integer wrapper to float or primitive type int to float?

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 295

Hi Ragi,

Can you please post your code here , that will make things clear. you can definitely assign an integer to float variable because its comes under float range .


SQL Query Interview Question java67 Javarevisited
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
ragi singh wrote:hi

i have an integer and i cant understand why i can assign its value to a float without casting ?

Top of my head:

int takes up less space in memory than float. I believe that Head First Java explained this beautifully when they mentioned cup size. int is a smaller cup than float, hence you can pour the contents of a small cup into a big cup without fear of it spilling over.

Marriage Made in Heaven
Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
funny thing is, both int and long both fit into a float, despite long being a 64 bit integer. Ie: it can still be converted into a float without casting, even though a float is 32 bits.... go figure, but I believe the explanation is that "floats are weird" :P

// Andreas
wood burning stoves
subject: int to float conversion
It's not a secret anymore!