Java - double type special value

Introduction

The double data type defines two zeros, two infinities, and a NaN.

The following table lists these constants and their meanings.

double Constants Meaning
Double.POSITIVE_INFINITY Positive infinity of type double
Double.NEGATIVE_INFINITY Negative infinity of type double
Double.NaN Not 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 10^308 (approx.)
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.

Demo

public class Main {
  public static void main(String[] args) {
    double d1 = Double.POSITIVE_INFINITY; 
    double d2 = Double.NEGATIVE_INFINITY; 
    double d3 = Double.NaN; 
    double d4 = Double.MAX_VALUE; 
    double d5 = Double.MIN_VALUE; 

    System.out.println(d1);/*from w  ww. j a va  2 s.  c o  m*/
    System.out.println(d2);
    System.out.println(d3);
    System.out.println(d4);
    System.out.println(d5);
    
  }
}

Result

Cannot assign a double literal to a double variable greater than the maximum value of double (1.7E308 approx)

// A compile-time error. 
double dTooBig = 8.8E308; 
  

Cannot assign a double literal to a double variable less than the minimum value (greater than zero) of double 4.9E-324.

// A compile-time error. 
double dTooSmall = 4.9E-325;