• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

int to float conversion

 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi


i have an integer and i cant understand why i can assign its value to a float without casting ?
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to convert Integer wrapper to float or primitive type int to float?
 
Javin Paul
Ranch Hand
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Andreas Svenkson
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic