• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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 showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic