
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
When can a double-type be preferred over float-type in Java?
In Java, both double and float are data types that are used to declare a variable that can store only decimal values or floating point numbers. For example: 1.2343, 3.343, 24.321234, etc. The double data type is larger in size compared to the float data type.
When to Prefer Double Type Over Float Type
A double-type is preferred over a float-type if a more precise and accurate result is required. The precision of double-type is up to 15 to 16 decimal points, while the precision of float type is only around 6 to 7 decimal digits.
Following are the syntaxes for declaring double and float variables in Java:
double varName = decimal_value; or float varName = decimal_valuef;
Here, varName is the name of the variable, decimal_value is the decimal value assigned to varName, and f or F specifies the float value.
Below is the size table of double and float data types in Java:
DataType Name | Size in Byte | Size in Bit |
---|---|---|
double | 8 | 64 |
float | 4 | 32 |
Here are the few differences between the double and float data types:
- The double is used for precise calculations and temp variables, whereas float is used for arrays only when memory optimization is needed.
- A double-type uses 1 bit for a sign and 11 bits for an exponent, whereas a float-type only uses 1 bit for a sign and 8 bits for an exponent.
- The default value of double-type is 0.0d, whereas the default value of float-type is 0.0f.
Example 1
The following is a basic example of using the double and float data types. In this program, we declare and initialize two variables (double and float type) with the same value 55.637848675695785, but when you display them, their values will be different:
public class DoubleFloatTest { public static void main(String []args) { double d = 55.637848675695785; float f = 55.637848675695785f; System.out.println("Value of double: " + d); System.out.println("Value of float: " + f); } }
The above program produces the following output:
Value of double: 55.637848675695786 Value of float: 55.637848
Example 2
This is another example of when to prefer to use the double type over the float type. When initializing a float value, you must add an 'f' at the end of the number; otherwise, you may get an error. However, with the double type, there is no need to specify any postfix:
public class DoubleFloatTest { public static void main(String []args) { double d = 0.0; float f = 0.0; System.out.println("Value of double: " + d); System.out.println("Value of float: " + f); } }
Following is the output of the above program:
DoubleFloatTest.java:4: error: incompatible types: possible lossy conversion from double to float float f = 0.0; ^ 1 error error: compilation failed