This week's book giveaway is in the Testing forum.We're giving away four copies of The Way of the Web Tester: A Beginner's Guide to Automating Tests and have Jonathan Rasmusson on-line!See this thread for details.
Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Difference between float f & double

sahan thinusha
Greenhorn
Posts: 10

Matthew Brown
Bartender
Posts: 4568
9
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
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
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.