• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Long to Float

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why doesn't this code give an error>
long m;
float f =m;???
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
where did you put that code ???
can you be more specific !
stevie
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Going from a long to a float is a widening conversion, so it's legal, even if there are more bits in a long than a float.
 
Pratiti Naphade
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why doesn't this code give an error>
public satic void main(String a[]){
long m=5;
float f =m;???
float is 32 in size and long is 64bits...
but it still compiles and does not give any error
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why doesn't this code give an error>
public satic void main(String a[]){
long m=5;
float f =m;???
float is 32 in size and long is 64bits...
but it still compiles and does not give any error
Pl.carefully read Com.Ref.Java2,Schidt.It is clearly mentioned that the width of data types is nothing to do with their storage,but certainly & only with their behavoiur.Hence in above case direct widening conversion takes place,legal in Java.
Long ,even if 32 bit,ignores the 0's and takes into consideration only whole value of th egiven int.In case of float which is necesarily an int with decimal part,can very
take care of lomg with assingnged in above way.
Hope if u r clear.
Pl.correct me if I am wrong.
 
You've gotta fight it! Don't give in! Read this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic