difference between float and double

Difference Between Float And Double Datatypes In Java

In Java, the datatypes double and float are used. Despite the fact that they both represent floating-point literals, there are a few minor differences between them. In this post, I’ll explain the differences between float and double in Java, as well as when to use float over double or double over the float, as well as some code examples.

  • Numeric data can be represented in two ways in computing: fixed-point and floating-point arithmetic. Fixed point data is defined as an integer multiplied by a scaling factor. For example, with a scaling factor of 100, 3.14 becomes 314, and 2.3456 becomes 2.345 to achieve a fixed number of digits. This method reduces the accuracy of the result and is not appropriate in all circumstances.
  • Using floating-point representation for high-precision calculations is thus easier and more accurate.

What is the difference between the datatypes float and double?

Double is more precise than float and can store 64 bits, which is twice as many as a float. We prefer double over float for storing large numbers because it is more precise. We can use float in most applications unless we need precision up to 15 or 16 decimal points, as double is more expensive.

Below is a small table that shows the memory requirement as well as the range of float and double values:

Floating point typeMemory requirementRange
Float4 bytes±3.40282347E+38F i.e. 6-7 significant digits
Double8 bytes±1.79769313486231570E+308 i.e. 15-16 significant digits

In all programming languages, float and double work in the same way. In Java, for example, both will throw NumberFormatException when no actual numbers are involved in the operation. This exception will not be detected by the compiler.

String s = “DR”;
float fname = Float.parseFloat(s);
System.out.println(fname/num1); // Leads to NumberFormatException at runtime
Dividing float and double by zero will give an output of ‘Infinity’ in Java.
double num = 344.55555555;
System.out.println(num/0);

The double-takes up more space but is more precise during computation, whereas the float takes up less space but is less precise. Float is a 32-bit representation of a real number, while double is a 64-bit representation, according to IEEE standards. The double data type is commonly used in Java programs.

  • Memory: Float requires 4 bytes (32 bits), whereas double requires 8 bytes (64 bits). The java program to print size a float and double takes is shown below. For float:

public class Float {
public static void main(String args[]) {
System.out.print(“float size is = “);
System.out.print(Float.SIZE + ” bits ” + ” or “);
System.out.println(Float.SIZE/8 + ” BYTES”); //// 1 BYTE = 8 bits
}
}

Output :

float size is = 32 bits or 4 BYTES

For double:

public class Double {
public static void main(String args[]) {
System.out.print(“double size is = “);
System.out.print(Double.SIZE + ” bits ” + ” or “);
System.out.println(Double.SIZE/8 + ” BYTES”); // 1 BYTE = 8 bits
}
}

Output :

double size is = 64 bits or 8 BYTES

Suffix: In Java, float numbers are double by default. You must explicitly add the suffix ‘f’ or ‘F’ to store them in a float variable.

  • Assume we have the number 1.23456. What happens if we store it in the float variable num?

float num = 1.23456 // compiler error

  • There are two ways to fix the compiler error mentioned above.
  • To begin, append ‘f’ or ‘F’ to the end of the number.

float num = 1.23456f
// or
float num1 = 1.23456F

Precision: A single-precision floating-point operation is a float. In other words, a float can provide the precision of 6-7 decimal points.
The operation double is a double-precision floating-point operation. In other words, double can provide the precision of 15-16 decimal points.

public class Float {
public static void main(String args[]) {
float num = 1.2345678f;
System.out.println(“float precision upto 6-7 digits after decimal : “);
System.out.print(num);
}
}

Output:

float precision up to 6-7 digits after the decimal :
1.2345678

For Double:

public class Double {
public static void main(String args[]) {
double num = 1.2345678910111213;
System.out.println(“double precision upto 15-16 digits after decimal : “);
System.out.print(num);
}
}

Output:

double precision upto 15-16 digits after decimal :
1.2345678910111213

  1. Data loss: Because float has a smaller range than double, there will be no data loss when it is converted to double.
    When converting a double to a float, data loss is to be expected.
  2. Default datatype: The default data type of floating point literals is not float.
    The default data type for floating point literals is double.
  3. Wrapper class: Wrapper class for float is java.lang.Float. Wrapper class for double is java.lang.Double.
  4. Default value: The default value for a float is 0.0f, and the default value for a double is 0.0d.

Numeric data can be represented in two ways in computing: fixed-point and floating-point arithmetic. Fixed point data is defined as an integer multiplied by a scaling factor. For example, with a scaling factor of 100, 3.14 becomes 314, and 2.3456 becomes 2.345 to achieve a fixed number of digits. This method reduces the accuracy of the result and is not appropriate in all circumstances.

What are literals?

  • A literal is a constant value that is assigned to a variable. Literals, in other words, are constants. Integer literals, boolean literals, Character literals, String literals, and floating-point literals are the five types of literals in Java.
  • Floating-point literals include float and double.

Numeric data can be represented in two ways in computing: fixed-point and floating-point arithmetic. Fixed point data is defined as an integer multiplied by a scaling factor. For example, with a scaling factor of 100, 3.14 becomes 314, and 2.3456 becomes 2.345 to achieve a fixed number of digits. This method reduces the accuracy of the result and is not appropriate in all circumstances.

Float vs. Double: A Side-by-Side Comparison

Now that we understand the differences between float and double, we can create a table of differences for quick reference and review.

FloatDouble
Single precision valueDouble precision value
Up to 7 significant digits can be stored Up to 15 significant digits can be stored
It takes up to 4 bytes of memory (32 bits IEEE 754)It takes up to 8 bytes of memory (64-bits IEEE 754)
If there are more than 7 digits in a value, it is rounded off.7-15 digits are stored in their current format.
To declare float –float fnum = 2.344f, orfloat fnum = (float) 2.344 in Java, typecast is required.Double is the default decimal point type for Java.double dnum = 2.344;
If high precision is not required and the program only requires a large array of decimal numbers to be stored, float is a cost-effective and memory-saving method of storing data.When more precision is required, double is more expensive, takes up more space, and is more effective. Currency conversion, financial reports and transactions, scientific calculations, and so on.
Suffix‘f’ or ‘F’‘d’ or ‘D’
Data LossWhen attempting to convert a float to a double, there is no data loss.If you try to convert a double to a float, you will lose data.
Default DatatypeNoYes
Wrapper classjava.lang.Floatjava.lang.Double
Default Value0.0f0.0d

Conclusion

This is the primary distinction between Double and Float, while typecasting from float to double and double to float is perfectly permissible and valid, it should be done with caution in the code. If you convert too frequently, precision may be lost, and the entire purpose of using double will be defeated. During the early stages of development, decide whether you want to use float or double and stick with it throughout the application. It’s also a good idea to understand how specific data is stored in the database. If your application must be performant, use float instead of double because double may cause your program to run slowly with large data sets. If your data requires greater precision, use double.

You may like:

Try-with-resources in java with Example

Java enum with Example

Java enum Constructor with Example

Java Inheritance with Example | Types of inheritance

Java final Keyword with Example

Not Equal Example in Java

Java Polymorphism with Example

Singleton Class in Java with Implementation and Example

Java Nested and Inner Class with Example

Hope this article will guide you to recognize all about difference between float and double that you needed and still if you have any problem or queries regarding this, post them in the comments section and we will be glad to assist you.

Leave a Reply