Java Difference Between Float and Double Data Types


A quick guide understanding the differences between float and double in java.

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.


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);



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'.


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);




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.


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);




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.


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);

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.


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);

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


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);


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.



About Us

Author: Venkatesh - I love to learn and share the technical stuff.

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,
JavaProgramTo.com: Java Difference Between Float and Double Data Types
Java Difference Between Float and Double Data Types
A quick guide understanding the differences between float and double in java.
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content