**Float and Double in Java**

Java is one of the most popular programming language that is used by many developers across the globe. Java programming language is a statically-typed, programming language which means that all variables must first be declared before they can be used. This involves stating the variable’s data type.

For Example if you want a numeric variable named “Age “ you need to tell your program that a field named Age which need to hold numerical data, and has an initial value of “23”. A variable’s data type determines the values it may contain,the default value it has taken , plus the operations that may be performed on it.

**Code:**

`int Age = 23;`

In addition to the numeric datatype **int** , the Java programming language supports seven other datat types among which we will concentrate only on two data types Float and Double.

**Float:**

The Float data type is a single-precision 32-bit IEEE 754 Floating point which is used to represent Floating point numbers in Java. The default value of Float is 0.0f. The Float is 32 bits of storage and has a range of 3 .4e–038 to 3.4e+038. The Float values are represented with 7 decimal digits.Variables of type Float** **are useful when you need a fractional component, but don’t require a large degree of precision. For example, Float** **can be useful when representing the currencies such as dollars and cents.

**Code:**

public class Float_Example {public static void main(String args[]){Float value = 10.0f;System.out.println(“The given Float number is : “ + value);}}

**Output:**

`The given Float number is : 10.0`

We can see that we have given an “f” literal at the end of our code. This is because we need to specify that the current value is Float in the same way as we use “e” for exponents. Give it a try by removing the literal, you will get the below error.

**Error:**

`java: incompatible types: possible lossy conversion from Double to Float`

This error tells you that it is not possible tyo convert Double to float as you have not used any literal.

**Double:**

The Double data type is a single-precision 64-bit IEEE 754 Floating point which is used to represent Floating point numbers in Java similar to Float data type. The default value of Double is 0.0d. The Double is 64 bits of storage and has a range of 1 .7e–308 to 1.7e+308. The Double values are represented with 15 to 16 decimal digits. All mathematical functions, such as sin( ), cos( ), and sqrt( ), return Double values.

**Code:**

// Compute the area of a circle.class Area {public static void main(String args[]) {Double pi, r, a;r = 3.34567; // radius of circlepi = 3.1416; // pi, approximatelya = pi * r * r; // compute areaSystem.out.println(“Area of circle is “ + a);}}

**Output:**

`Area of circle is 35.165523943944244`

As we have seen how these two datatypes work, let’s see some of their similarities.

**Similarities:**

a) Both are used to represent real numbers in Java and numbers with fractions or decimal points.

b) Both are approximate types, they are not precise.

c) You should use logical operator like > or < to compare both variables, instead of = and != because they are not precise.

Let’s now look at some of the differences between them.

**Differences:**

a) Double is more precise than Float in Java.

b) Double takes more storage space when compared to Float in Java.

c) Double has a higher range when compared to Float.

d) Float uses 1 bits for sign, 8 bits for exponent and 23 bits for mantissa, while Double uses 1 bits for sign, 11 bits for exponent and 52 bits for mantissa which is a very good difference between them.

e) By default, Floating point numbers are Double in Java.

**Conclusion :**

In this article we have seen what are Float and Double datatypes, though both can be used to represent floating point numbers, there are couple of things you can consider to choose between double and float. If you need more precise and accurate result then use double over float . Use float if you have memory constraint . If your numbers cannot fit in range offered by float then use double as it has a better range.