• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

regarding float and long

 
Greenhorn
Posts: 25
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
float has 4 bytes and long has 8 bytes but i faced a problem
float f = 10L; is valid..
why it is possible?
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you read the Java Language Specification section which covers this question? It is not easy to read, I am afraid.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harivenkatesh Polnati wrote:float has 4 bytes and long has 8 bytes but i faced a problem
float f = 10L; is valid..
why it is possible?




To summarize it, basically, when it comes to implicit casting, the JLS is only concerned about range (and not about precision). In the case of float, even though it is smaller in footprint than a long, it has a larger range than long (even though the specification doesn't count infinity and negative infinity).

Of course, with much lower precision, there are lots of numbers that can be represented by the long, that is only "rounded off" by the float.

Henry
 
Harivenkatesh Polnati
Greenhorn
Posts: 25
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for your answers
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
That’s what it meant in the JLS link about losing information
 
reply
    Bookmark Topic Watch Topic
  • New Topic