File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between float f & double

 
sahan thinusha
Greenhorn
Posts: 10
Android Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Please explain about these commented lines..
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you want to know about them? float and double are both floating point primitive types, but double uses twice as many bits to store the value and so can store numbers more precisely.
 
sahan thinusha
Greenhorn
Posts: 10
Android Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
f=0.123456789123456789F;//compile
but, f=0.123456789123456789;//compile error
why?
and (f=0.1234567......F;)=(d=0.1234567.........;) or (f=0.1234567......F;)<(d=0.1234567.........;) please explain this.






 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sahan thinusha wrote:f=0.123456789123456789F;//compile
but, f=0.123456789123456789;//compile error
why?


A floating point literal in Java is a double unless you tell the compiler otherwise with the F. So it's like you're doing and when you assign a double value to a float variable, you can lose precision, so the compiler requires you to cast.

and (f=0.1234567......F;)=(d=0.1234567.........;) or (f=0.1234567......F;)<(d=0.1234567.........;) please explain this.


It's not really clear what you're asking, but if you're asking whether a float will be equal to a less than a double intiialized to the same literal, the answer is, it depends. If the number can be represented exactly in a float, they'll be equal. If it cannot be, they might be different, because the double has more precision.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic