Datatypes in Java are the type of data that tells the compiler how the programmer plans to use the data. It is divided into the following two categories:
- Primitive Datatypes
- Non-Primitive Data types
Primitive Data types are the ones that include predefined data types such as int, float, char, etc and Non-Primitive Data types are the ones that include user-defined data types such as Arrays, Interfaces, etc. The floating-point data type is one of the Primitive data types. This floating-point data type is again divided into two types: float data type and double data type.
Though both the float and double data types are used to represent floating-point numbers still, they are different.
Difference Between Float and Double in Java
These are the following differences between float and double:
- float is a single-precision 32 bit IEEE 754 floating-point whereas double is a double-precision 64 bit IEEE 754 floating-point.
- The double data type is more precise compared to the float data type.
- The double data type provides precision up to 15 to 16 decimal points whereas float provides precision up to 6 to 7 decimal points.
- The float data type takes less memory compared to the double data type.
- The double data type has a higher range compared to the float data type.
- When we convert float to double there is no chance of data loss whereas when we convert double to float there is a high chance of data loss.
- The default data type for floating-point data types is double.
- While representing a double data type suffix 'd' or 'D' is used whereas for a float data type suffix 'f' or 'F' is used.
- It is mandatory to add a suffix while declaring a float data type whereas it is optional to add a suffix while declaring a double data type.
- The double data type is used where we require more accuracy whereas the float data type is used where we require less accuracy.
- The default value of float is 0.0f whereas the default value of double is 0.0d.
- The wrapper class for float is java.lang.Float whereas for double it is java.lang.Double.
Float Vs Double: Comparison Table
To sum up, all the above points, let us look at the comparison table for easy understanding:
Basis of Comparison |
float |
double |
Memory |
32 bits or 4 bytes |
64 bits or 8 bytes |
Default Value |
0.0f |
0.0d |
Default Datatype |
No |
Yes |
Precision |
Less about 6-7 decimal points |
More about 15-16 decimal points |
Suffix |
f or F |
d or D |
Wrapper Class |
java.lang.Float |
java.lang.Double |
Data Loss |
No, while converting float to double |
Yes while converting double to float |
Keyword used |
float |
double |
Let us look at the example for each of the floating-point data types for better understanding:
Example: float data type in Java
//Java float data type
public class Main
{
public static void main(String[] args)
{
float a=7.0f;
float b=9.0f;
float res=a/b;
System.out.println("Resulting float value "+res);
//float c=3.0; //incompatible types: possible lossy conversion from double to float
System.out.println("Size of float: " + (Float.SIZE/8) + " bytes.");
System.out.println("Size of float: " + (Float.SIZE) + " bits.");
}
}
Resulting float value 0.7777778
Size of float: 4 bytes.
Size of float: 32 bits.
Example: double data type in Java
//Java double data type
public class Main
{
public static void main(String[] args)
{
double a=7.0d;
double b=9.0d;
double res=a/b;
System.out.println("Resulting double value "+res);
double c=3.0; //No error
System.out.println("double value without suffix "+c);
System.out.println("Size of double: " + (Double.SIZE/8) + " bytes.");
System.out.println("Size of double: " + (Double.SIZE) + " bits.");
}
}
Resulting double value 0.7777777777777778
double value without suffix 3.0
Size of double: 8 bytes.
Size of double: 64 bits.
Conclusion
From the above discussion, it is clear that both float and double are two different floating-point data types that are used in different places depending on their requirement. The float data type is generally used where we require less accuracy whereas double data type is generally used in places where we require higher precision. For example, while dealing with monetary issues, it is generally preferred not to use floating precision as the smaller variations can lead to higher inaccuracies while calculating the amount.