The floating data type represent the floating numbers that means numbers with decimal point .For example: 1.45, 3.141, 0.005, -125.12 etc. There are 2 types of floating datatypes such as float and double which represent single and double precision numbers respectively.
The following table shows the memory size and range of float and double.
|1||Float||4bytes||-3.4e38 to -1.4e-45 for ?ve values and 1.4e-45 to 3.4e38 for +ve values|
|2||Double||8bytes||-1.8e308 to -4.9e-324 for negative values and 4.9 e -324 to 1.8e308 for positive values|
Float can represent up to 7digit accurately after decimal point, where as double can represent up to 15 digit accurately after decimal point.
Let us consider this statement
Here the variable x contains the value 1.25.
If f is not written at the end, then JVM will allocate 8 bytes because JVM will assume the values double. The reason is in java in float and double datatypes, the default it is taken as double. So for f, JVM consider it as float and allocate 4bytes of memory.
Let's see a simple program which uses double variable to fined the area of circle.
area is 15❮Previous Next ❯