Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Monday, December 20, 2021

Java Difference Between Float and Double Data Types

1. Overview

In this tutorial, We'll learn what are the differences between float and double data types in java.

Float and double data types are used to represent the floating-point values but there are few differences and you must know all of these when using them. 

First, let us understand float vs double and then next when to use which double and float?

Any fixed value is assigned to a variable is called literal in java. double and floating data storage variables are called Floating literals.

All examples shown are placed in GitHub and a link is given at the end of the article.


Java Difference Between Float and Double Data Types




2. Difference float vs double 1 - Memory


In terms of memory, double primitive data type takes 4 bytes ( 4 * 8 = 32 bits) and float primitive data type takes 8 bytes (8 * 8 = 64 bytes).

Let us write the simple example program to get the memory size float vs double in bytes and bits.

Example

package com.javaprogramto.datatypes.diff;

public class FloatVsDoubleMemory {

	public static void main(String[] args) {

		System.out.println("Double size");
		int doubleSizeBits = Double.SIZE;
		int doubleSizeInBytes = doubleSizeBits / 8;

		System.out.println("Double size in bytes - " + doubleSizeInBytes);
		System.out.println("Double size in bits - " + doubleSizeBits);

		System.out.println("\n" + "Float size");
		int floatSizeBits = Float.SIZE;
		int floatSizeInBytes = doubleSizeBits / 8;

		System.out.println("Float size in bytes - " + floatSizeInBytes);
		System.out.println("Float size in bits - " + floatSizeBits);

	}

}

Output
Double size
Double size in bytes - 8
Double size in bits - 64

Float size
Float size in bytes - 8
Float size in bits - 32

3. Difference float vs double 2 - Suffix


The suffix is very important to denote the float data types values. Because by default, decimal type values are considered as double type.

But when you assign a decimal value to double primitive time then you no need to explicitly define any suffix to it because default decimal type is double.

If we try to assign a number like 1.2345 to float type then it will give the compile time error.

To fix the float assignment issue, we need to explicitly add the suffix 'f' or 'F' to the number.

Alternatively, we can type cast to float from double value.

For double, optionally you can add suffix as 'd' or 'D'.

Example

Look at the below example to understand clearly suffix concept.

package com.javaprogramto.datatypes.diff;

public class FloatVsDoubleSuffix {

	public static void main(String[] args) {

		// double examples
		double d = 10.9876;

		System.out.println("Double value no explicit usage of suffix - " + d);

		double d2 = 1.23456789d;
		System.out.println("Double value optionnal suffix d - " + d2);

		// float examples

		// Compile time error
		// float f3 = 123.456; // Type mismatch: cannot convert from double to float

		float f = 10.9876f;

		System.out.println("\n" + "Float value with explicit usage of suffix f - " + f);

		float f2 = (float) 1.23456789;
		System.out.println("float value with type casting - " + f2);

	}

}

Output

Double value no explicit usage of suffix - 10.9876
Double value optionnal suffix d - 1.23456789

Float value with explicit usage of suffix f - 10.9876
float value with type casting - 1.2345679


4. Difference float vs double 3 - Precision


The core difference between float vs double is the precision. How many decimal points can be preserved without losing fractions.

float type variable can give accurately 6 to 7 points precision because the float is single-precision floating-point operation
whereas double type variable can give accurately up to 15 to 16 points precision because double is double-precision floating point operation.

Example

Let us create a simple example to understand clearly the precision concept.

package com.javaprogramto.datatypes.diff;

public class FloatVsDoublePrecision {

	public static void main(String[] args) {

		// double examples
		double d = 1.2345678912345678;

		System.out.println("Double type precision value 1 - " + d);

		double d2 = 1.1020304050d;
		System.out.println("Double type precision value 2 with suffix d - " + d2);

		// float examples

		float f = 1.23456789123456789f;

		System.out.println("\n" + "Float type precision value 1 - " + f);

		float f2 = (float) 2.10203040;
		System.out.println("Float type precision value 2 with suffix d - " + f2);

	}

}

Output

Double type precision value 1 - 1.234567891234568
Double type precision value 2 with suffix d - 1.102030405

Float type precision value 1 - 1.2345679
Float type precision value 2 with suffix d - 2.1020305

5. Difference float vs double 4 - Data Loss


Double has more capacity to store decimal points and with most precision but float has less capacity and precision.

When the float is converted into double there won't be a loss of precision and data but when double is converted into afloat, we can see the data loss.

No data loss from float to double and data loss from double to float.

Example

The below example demonstrates the data loss float vs double.

package com.javaprogramto.datatypes.diff;

public class FloatVsDoubleDataLoss {

	public static void main(String[] args) {

		// double examples
		double d = 1.2345678912345678;

		float f = (float) d;

		System.out.println("float value after double to float conversion (data loss) - " + f);

		// float examples
		float f1 = 1.1111111f;

		double d1 = f1;

		System.out.println("\n" + "double value after float to double conversion - " + d1);
	}
}


Output
float value after double to float conversion (data loss) - 1.2345679

double value after float to double conversion - 1.111111044883728

6. Difference float vs double 5 - Default Type and Value


Default type: when the number is with the floating points then it is considered as the double and float type is not the default type for the floating points or real numbers.


Default Value: double type default value of 0.0 and float type default value is 0.0 if no values are assigned in the declaration.

Example

package com.javaprogramto.datatypes.diff;

public class FloatVsDoubleDefault {

	static float floatDefaultvalue;
	static double doubleDefaultvalue;

	public static void main(String[] args) {

		// default types
		double d = 12345.6789; // no error. so it is considered as double type.

		//float f = 12345.6789; // error. Type mismatch: cannot convert from double to float. float type is not
								// detected

		// default values

		System.out.println("Float type default value - " + floatDefaultvalue);
		System.out.println("Double type default value - " + doubleDefaultvalue);
	}
}

Output
 
Float type default value - 0.0
Double type default value - 0.0

7. Difference float vs double 6 - Wrapper Type


float and double primitive types have equivalent wrapper types which can also be used to create the double and float real numbers without explicit suffix f or d.

float wrapper class java.lang.Float
double wrapper class java.lang.Double


Example

package com.javaprogramto.datatypes.diff;

public class FloatVsDoubleWrapper {

	public static void main(String[] args) {

		// Double wrapper
		Double d1 = new Double(123.456);
		System.out.println("Double d1 value - " + d1);

		Double d2 = new Double("7.890");
		System.out.println("Double d2 value - " + d2);

		Double d3 = Double.valueOf(12.456);
		System.out.println("Double d3 value - " + d3);

		// Float wrapper
		Float f1 = new Float(123.456);
		System.out.println("\n" + "Float f1 value - " + f1);

		Float f2 = new Float("7.890");
		System.out.println("Float f2 value - " + f2);

		Float f3 = Float.valueOf(12.456f);
		System.out.println("Float f3 value - " + f3);

	}
}


Output
Double d1 value - 123.456
Double d2 value - 7.89
Double d3 value - 12.456

Float f1 value - 123.456
Float f2 value - 7.89
Float f3 value - 12.456


8. Similarities Between Float vs Double


8.1 Both double and float are used to store the real numbers that is numbers with fractions and floating points.

8.2 float and double have the wrapper classes Float and Double. Both wrapper classes are present in java.lang package.

8.3 float and double provide approximate values but not exact precision for float.


9. When to use float and when to use double?


double data type provides more precision than float up to 16 - 17 precision points precision. Otherside float provides just 7 precision points maximum.

You need to remind that double take more memory to store the accurate precision. This has to be preferred only when you want to store more no of fractions.

Always prefer double over float if you do not have any memory constraint as it gives more precision.

But if you have memory concerns then use float.

But the recommended practice is to choose float or double based on your use case. If you need more precision then go for the double else float.


10. Conclusion


In this article, We've seen the core differences between double and float in java with examples on differences.







No comments:

Post a Comment

Please do not add any spam links in the comments section.