One of the most common questions I get is about the difference between floats and doubles, in Java and other programming languages alike. Here is the explanation I posted on a video of mine a few weeks ago:
“When you create any variable in Java, you’re using up memory. For example, when you create an “int,” you’re using up 32 bits of memory. When you create a “float,” you’re also using up 32 bits of memory. However, a “double” is literally a “double” float – you’re using up 64 bits of memory. Often times doubles are referred to as double-precision floats because you have twice as much memory to work with. This memory adds both size and precision to doubles:
With a float, you can accurately get about 7 decimal places before the system starts to lose precision. With a float, you have about 16 decimal places of precision.
So for example, if you have: 65.389745835897357982347892350789
And you create:
float f1 = 65.389745835897357982347892350789;
double d1 = 65.389745835897357982347892350789;
And then you try to access both of these variables:
You would get output like:
Notice where the decimal places start to lose accuracy.
With the float: 65.3897458(11737495830495837462584)
With the double: 65.3897458358973579(29384628374058)
So doubles can handle more decimal places, in a nutshell.
Also, the extra memory allows doubles to handle larger numbers in general. Floats can already handle numbers from -3.4E+38 to +3.4E+38, but doubles can handle -1.7E+308 to +1.7E+308. If that’s confusing, don’t worry. Again, in a nutshell, both can handle very large numbers; floats can simply handle large numbers, while double can handle VERY large numbers.
If you’re deciding between whether to use a float or double, for beginner purposes, just go with double. You’ll more than likely have enough computing power to handle a 64 bit variable.
I hope that clears things up!”