Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Long to Float

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic