Question

I'm a uni student and have started studying programming in java and as far as I can tell both float and double do the same thing, they are both for writing integers decimal places. However I feel I'm completely wrong as there obviously wouldn't be 2 commands to achieve the same thing. Can someone please explain if there is a difference and if possible expand on it by saying why a float wouldn't work in a double situation and vice versa.

P.S. I'm not asking for answers to uni quizzes or anything, I've got my name on here, just genuinely trying to wrap my head around the difference between float and double.

Cheers,

Chris.

Was it helpful?

Solution

A float is 4 bytes wide, whereas a double is 8 bytes wide. You should probably prefer to use double in most cases. You may check IEEE floating point standard.

Also to note that floats can be doubles, but doubles can never be floats because doubles have the larger range.

Range of float is 1.40129846432481707e-45 to 3.40282346638528860e+38

Range of double is 4.94065645841246544e-324d to 1.79769313486231570e+308d

OTHER TIPS

Floats take less space, but at the cost of less precision (floats are 4 bytes, doubles are 8). It's the same reason you'd chose an int over a long. Certain math operations might also be slower on doubles, but in practice, you probably want a double.

The only difference I know of is the precision, where float data type is a single precision 32-bit IEEE 754 floating point and double data type is a double precision 64-bit IEEE 754 floating point.

Hope this helped you get a better understanding of the difference.

Generally float uses 4 bytes and double uses 8 bytes to store the value in a floating point (mantissa + exponent) format/representation. See this wikepedia write-up for an intro to these concepts if you are interested: http://en.wikipedia.org/wiki/Floating_point

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top