Two Laptop Bag*
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "int to float conversion " Watch "int to float conversion " New topic
Author

int to float conversion

ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

hi


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

Joined: Aug 03, 2008
Posts: 9291
    
  17

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


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 .

Thanks
Javin


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
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
http://www.youtube.com/user/RohitWaliaWedsSonia
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: int to float conversion
 
Similar Threads
Tech Word Game
Why is this happening?
WA #2 ..... word association
Widening
Can anyone tell the result of this code.