- Float vs. double in Java
- The two representations of a floating-point number
- Difference between float and double variable in Java? Example
- float and double in Java — Similarities
- Difference between float vs doubles in Java
- When to use double and float in Java?
- Java: Float vs Double | 4 Main Differences (& when to use?)
- What is Float & Double?
- 4 Differences b/w Float and Double in Java
- Float vs Double in Java Comparison
- When to use Float and Double in Java?
- Conclusion
Float vs. double in Java
A data type in a programming language classifies and represents data in a particular category that determines the type of values that can be stored in a variable. Different programming languages offer different data types including integers, floating-point numbers, characters, strings, and boolean numbers. In this blog, we’ll focus on floating-point numbers, more precisely on the differences between float and double.
The difference between float and double is in their precision. Float is a 32-bit single-precision floating-point type, whereas double is a 64-bit double-precision floating point.
However, there is more to learn about these data types, such as how are they stored? What are their use cases? And more importantly, are there any differences between float and double in Java?
The two representations of a floating-point number
A number in a binary system consists of three parts:
- Sign: Represents whether the number is positive or negative. It is usually represented by a bit, with 0 0 0 indicating a positive and 1 1 1 indicating a negative number.
- Integer: Represents the whole number that appears before the decimal point.
- Fraction: Represents the fraction that appears after the decimal point.
Fixed-point representation of a binary number uses a fixed number of bits for integer and fraction parts. Although convenient, fixed-point representation has limited precision and depends on the number of bits allocated to the fractional part.
Take an example of a 16-bit representation of 3.1416 3.1416 3.1416 where we fix 7 7 7 bits for the integer and 8 8 8 bits for the fraction after the decimal point. The remaining 1 1 1 bit is reserved for the sign bit.
- The sign bit will be 0 0 0 since the number is positive.
- The integer 3 3 3 is converted into binary as 0000011 0000011 0000011 .
- The fraction 0.1416 0.1416 0.1416 is converted into binary as 00100100 00100100 00100100 .
Another approach could be to fix the number of bits for the number and another set of bits to indicate the position of the decimal point within that number. This is called floating-point representation. We call the number without a decimal point a mantissa and the position of the decimal place an exponent. Floating-point representation is beneficial for applications in which the range of the values is large and the precision requirements are high.
Difference between float and double variable in Java? Example
Though both float and double datatype are used to represent floating-point numbers in Java, a double data type is more precise than float . A double variable can provide precision up to 15 to 16 decimal points as compared to float precision of 6 to 7 decimal digits. Another significant difference between float and double is their storage requirement, double is more expensive than float. It takes 8 bytes to store a variable while float just takes 4 bytes. This means if memory is a constraint then it’s better to use float than double. By the way, the double type also has a larger range than float and if your numbers don’t fit well in float then you have to use double in Java.
It’s also worth noting that floating-point numbers or real numbers are by default double in Java. If you want to store them into the float variable, you need to either cast them or use the prefix ‘f’ or ‘F’ as shown in our example.
float and double in Java — Similarities
Here are some common attributes of both float and double data types in the Java programming language:
Both double and float are used to represent real numbers in Java i.e. numbers with fractions or decimal points.
Both double and float are approximate types, they are not precise.
See this article for more details about comparing float and double variables in Java.
Difference between float vs doubles in Java
Here are some key differences between float and double in Java :
1. The double data type is more precise than float in Java.
2. double-takes more space than float in Java. double needs 64-bit storage compare to 32-bit storage of float data type.
3. double has a higher range than float, sure because it got more bits to store data.
4. float uses 1 bit for sign, 8 bits for the exponent, and 23 bits for mantissa, while double uses 1 bit for a sign, 11 bits for the exponent, and 52 bits for mantissa.
5. By default, floating-point numbers are double in Java. In order to store them into a float variable, you need to cast them explicitly or suffix with ‘f’ or ‘F’ as shown below :
public static final float PIE = 3.14; // compile time error
public static final float PIE = (float) 3.14;
public static final float PIE = 3.14f; public static final float GRAVITY = 9.8F;
When to use double and float in Java?
Though both can be used to represent floating-point numbers, there are a couple of things you can consider to choose between double and float . Though both are approximate types, If you need more precise and accurate results then use double.
Use float if you have memory constraints because it takes almost half as much space as double. If your numbers cannot fit in the range offered by float then use double. Though be careful with floating-point calculation and representation, don’t use double or float for monetary calculation, instead use BigDecimal.
That’s all about the difference between float and double in Java. Remember, by default floating-point numbers are double in Java, if you want to store them into float variables, you need to either cast them explicitly or suffixed them using the ‘f’ or ‘F’ characters.
It’s also best practice to choose a data type that takes less storage if it’s sufficient for data you are storing, so choose float over double if you are happy with precision and range, double is more accurate than float though.
Java: Float vs Double | 4 Main Differences (& when to use?)
Every programming language deals with a wide range of data types. In Java, floating-point numbers are represented with the help of two data types: float and double. If you wish to dive deeper into the language of Java and know some of the important concepts of data types, then this article is definitely for you. Our article on the differences between float and double in Java should help you learn about the underlying concepts for the two data types. We also included a comparison table for float vs double in java programming.
What is Float & Double?
Broadly, data can be divided into two main types: numeric and non-numeric data. Non-numeric data include characters and strings. Numeric data consists of whole numbers and floating-point numbers. Whole numbers, like the name, says, have only a whole part – there is no fraction involved. Floating-point numbers, on the other hand, come with a fractional part and a whole part out of the box. Depending on the size of the data required, whole numbers are assigned different data types that can be used depending on the requirement. Similarly, depending on the size of the number — especially the fractional part: floating-point numbers can also be assigned different data types. Float and Double are used to represent Floating-point numbers.
Float and double are the two data types Java allocates for dealing with floating-point numbers. Both data types are primitive by nature: meaning that their definitions are included as part of the language itself (or more specifically, the java.lang package – the default import for any Java program). Why exactly are two data types allocated for doing the same job? Well, they vary a lot in their scope and usability in various scenarios.
Let’s have a look at how different these two data types are.
4 Differences b/w Float and Double in Java
So, here are 4 main differences between float and double in Java:
- Size: Float is of size 32 bits while double is of size 64 bits. Hence, double can handle much bigger fractional numbers than float. They differ in the allocation of bits for the representation of the number. Both float and double use 1 bit for representing the sign of the number. However, they differ in the allocation of bits for the mantissa and the exponent. Float allocates 23 bits for mantissa, while double allocates 52 bits for mantissa. Similarly, float uses 7 bits for exponent while double allocate 11 bits for exponent.
- Precision: Float can allow only 7 bits of precision. Double allocates almost double the number of bits for the exponent – allowing it to handle up to 15 bits of precision. Having an extremely small exponent size means there is some amount of data compression while working with the float. Data compression inevitably means some of the bits are lost. This loss of bits from the end of the number is very pronounced while using float. The number of bits lost is much lesser while using double because of the large number of bits allocated for the exponent.
- Default: Double is the default data type used by Java for dealing with fractional numbers. To force Java to use the float data type, the character ‘f’ or ‘F’ must be suffixed to the number. This can also be done by forcibly typecasting the number to float by prefixing (float) to the number at the time of initialization.
- Wrapper Class: Both float and double are primitive types. This means that they will have their wrapper classes which allow them to be used as object types. The wrapper class for float is java.lang.Float while the wrapper class for double is java.lang.Double.
Float vs Double in Java Comparison
The following infographic summarizes the differences between float and double in java:
When to use Float and Double in Java?
Float is generally used when speed is more important than accuracy. While our day-to-day programs do not involve large computations, in practical applications, this difference becomes quite significant. Also, the float is generally used when the number of decimal digits required is known. Double is used for almost all other cases: especially when precision is highly important. Keep in mind that Java enforces the use of double as the default data type for dealing with floating-point numbers.
Conclusion
Float and double are two of the data types allocated by Java for dealing with floating-point numbers (or fractional numbers). While they might seem similar, in reality, they are quite different from each other. Do you need help with understanding java’s float and double types? Our java online help experts are available. Our article on the key differences between these two data types summarizes Float vs Double in java programming.