## Understanding double and float data type

Double and float data type declaration its max value and sizing.

## Learning Objectives

- Declaration and initialisation of double and float types.
- Limits of values
- Maximum value
- Size in bits

## Source Code

`class DoubleFloat`

{

public static void main(String[] args)

{

Double x=2342.34567;

float y=2342.34567f;

System.out.println(x);

System.out.println(y);

Double X=x;

Float Y=y;

System.out.println("Double type max value = "+X.MAX_VALUE);

System.out.println("Float type max value = "+Y.MAX_VALUE);

System.out.println("Double type size = "+X.SIZE);

System.out.println("Float type size = "+Y.SIZE);

}

}

## Run Output

## Code Understanding

**Double x=2342.34567;**

Here we are declaring and initialising a double data type. Note that the value 2342.34567 doesn’t have any suffix.

**float y=2342.34567f;**

Here we are declaring and initialising a float data type. Note that the value 2342.34567f has a f suffix which indicate that store this value in 32 bits only even if there is loss of precision.

**System.out.println(x);**

This will print the value 2342.34567 as expected. This is due to the fact that double can allow upto 15 decimal digits of precision in its decimal representation form.

**System.out.println(y);**

This will print the value 2342.3457 as against 2342.34567 as expected. This is due to the fact that float can allow upto 7 decimal digits of precision in its decimal representation form. Here 8 digits are printed and the last digit is likely out of precision.

**Double X=x; Float Y=y;**

Here we are enveloping both double and float primitive types to its wrapper class variation so that the required methods and fields can be used.

**System.out.println(“Double type max value = “+X.MAX_VALUE);**

Here we print maximum possible positive value for a double number. this will have 15 digit precision and maximum scientific notation power of 308. This is actually a very large number.

**System.out.println(“Float type max value = “+Y.MAX_VALUE);**

Here we print maximum possible positive value for a float number. this will have 7 digit precision and maximum scientific notation power of 38.

**System.out.println(“Double type size = “+X.SIZE);**

The storage data size of double which is 64 bit is printed here. In its binary storage form 1 bit is reserved for sign, 8 bits for exponent and 23 bits are there to store the significant digits base part.

**System.out.println(“Float type size = “+Y.SIZE);**

The storage data size of double which is 32 bit is printed here. In its binary storage form 1 bit is reserved for sign, 11 bits for exponent and 52 bits are there to store the significant digits base part.

## Notes

- IEEE 754 standard specifies the precision and storage mechanism of fractional values in detail

https://en.wikipedia.org/wiki/IEEE_754 - The storage format of double data type is well explained at

https://en.wikipedia.org/wiki/Double-precision_floating-point_format - Float fields and methods can be referred from

https://docs.oracle.com/javase/7/docs/api/java/lang/Float.html - Double fields and methods can be referred from

https://docs.oracle.com/javase/7/docs/api/java/lang/Double.html

**Suggested Filename(s): **DoubleFloat.java

sunmitra| Created: 19-Mar-2018 | Updated: 19-Mar-2018|