• Post Reply Bookmark Topic Watch Topic
  • New Topic

regarding float and long  RSS feed

 
Harivenkatesh Polnati
Greenhorn
Posts: 25
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • 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?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your answers
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
That’s what it meant in the JLS link about losing information
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!