Java Data Type Tutorial - Java double Data Type








The double data type uses 64 bits to store a floating-point number. double value is also known as a double-precision floating-point number.

It can represent a number as small as 4.9 x 10-324 and as big as 1.7 x 10308 in magnitude.

It could be positive or negative.

All real numbers are called double literals. A double literal may optionally end with d or D, for example, 1.27d.

The suffix d or D is optional in double literals.

Both 19.7 and 19.7d represent the same double literal.

Double Literals

A double literal can be expressed in the following two formats:

  • Decimal number format
  • Scientific notation

Examples of double literals in decimal number format are as follows:

double   d1  = 8D ;
double   d2  = 8.; 
double   d3  =  8.0; 
double   d4  =  8.D; 
double   d5  =  78.9867; 
double   d6  =  45.0;

8 is an int literal whereas 8D, 8., and 8.0 are double literals.

We can also use scientific notation to express double literals.

double   d1  = 32.5E-1; 
double   d2  = 0.325E+1; 
double   d3  = 0.325E1; 
double   d4  = 0.0325E2; 
double   d5  = 0.0325e2; 
double   d6  = 32.5E-1D; 
double   d7  = 0.325E+1d; 
double   d8  = 0.325E1d; 
double   d9  = 0.0325E2d;




Note

The double data type defines two zeros, two infinities, and a NaN. They are represented by constants in the Double class.

The following table lists these constants and their meanings.

ConstantsMeaning
Double.POSITIVE_INFINITYPositive infinity of type double
Double.NEGATIVE_INFINITY Negative infinity of type double
Double.NaNNot a Number of type double
Double.MAX_VALUE The largest positive value that can be represented in a double variable. This is equal to 1.7 x 10308
Double.MIN_VALUE The smallest positive value greater than zero that can be represented in a double variable. This is equal to 4.9 x 10-324.

The value of all integral types (int, long, byte, short, char) and float can be assigned to a variable of the double data type without using an explicit cast.

     
int num1 = 15;
double   salary = num1;                    
salary = 12345;                            
double   bigNum = Double.MAX_VALUE;  
bigNum = 1234L;                      
double   justAChar = 'A';      

double   dInf = Double.POSITIVE_INFINITY;
double   dNan = Double.NaN;

A double value must be cast to the integral type before it is assigned to a variable of any integral data type (int, long, byte, short, or char).