Java double type creation and comparison

In this chapter you will learn:

  1. How to find max and min values for double type
  2. How to create double class with constructor
  3. How to find out if a double value is an infinite large value, or it is not a number

double type max/min values

Double class defines the following constants to stores its min, max value and its size.

  • static int MAX_EXPONENT stores maximum exponent a finite double variable may have.
  • static double MAX_VALUE stores largest positive finite value of type double.
  • static int MIN_EXPONENT stores minimum exponent a normalized double variable may have.
  • static double MIN_NORMAL stores smallest positive normal value of type double.
  • static double MIN_VALUE stores smallest positive nonzero value of type double.
  • static double NaN stores Not-a-Number (NaN) value of type double.
  • static double NEGATIVE_INFINITY stores negative infinity of type double.
  • static double POSITIVE_INFINITY stores positive infinity of type double.
  • static int SIZE stores the number of bits used to represent a double value.

The following code finds out the min/max value a double type variable can have. It also prints out the size of a double value.

public class Main {
  public static void main(String[] args) {
    System.out.println(Double.MAX_VALUE);
    System.out.println(Double.MIN_VALUE);
    System.out.println(Double.SIZE);// java 2  s .  c  o  m

  }
}

The output:

public class Main {
  public static void main(String[] args) {
    System.out.println(Double.MAX_EXPONENT);
    System.out.println(Double.MIN_EXPONENT);
    System.out.println(Double.MIN_NORMAL);
/*from   java2s . c o m*/
  }
}

The output:

The following code uses the Double.NEGATIVE_INFINITY to check a double value.

public class Main {
  public static void main(String args[]) {
//from ja  v a  2s  .c o  m
    double d = -10.0 / 0.0;
    if (d == Double.NEGATIVE_INFINITY) {
      System.out.println("d = " + d);
    }

  }
}

The output:

Create Double class with Constructor

We can create a double type object with the following two constructors.

  • Double(double value) creates a Double object that represents the primitive double argument.
  • Double(String s) creates a Double object that represents the double value represented by the string.

The following code creates two Double type object and one is from double primitive type and another is from a string value.

public class Main {
  public static void main(String[] args) {
    System.out.println(new Double(1.1));
    System.out.println(new Double("1.2"));
//from ja  va 2  s. c  om
  }
}

The output:

Is a double value an infinite large value, or it is not a number

isInfinite() returns true if the value being tested is infinitely large or small in magnitude. isNaN() returns true if the value being tested is not a number.

  • boolean isInfinite() returns true if this Double value is infinitely large in magnitude, false otherwise.
  • static boolean isInfinite(double v) returns true if the specified number is infinitely large in magnitude, false otherwise.
  • boolean isNaN() returns true if this Double value is a Not-a-Number (NaN), false otherwise.
  • static boolean isNaN(double v) returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.

The following code divides 1 by 0 and creates a infinite double value and then its uses the isInfinite() to check.

public class Main {
  public static void main(String[] args) {
    Double double1 = new Double(1/0.0);
    //from ja  v  a 2 s  .com

    System.out.println(double1.isInfinite());

  }
}

The output:

public class Main {
  public static void main(String[] args) {
    Double double1 = new Double(0.0/0.0);
    /*from   jav a  2  s  .  com*/
    System.out.println(Double.isNaN(double1));
    System.out.println(double1.isNaN());

  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to convert double value as byte, double, float, int, long, short
  2. How to convert string value to double
  3. How to convert double value to string
  4. How to get the hexadecimal string representation
  5. How to do bit oriented calculation for double type
Home » Java Tutorial » Primitive Data Types

Introduction

    Java Primitive Data Types

Boolean

    Java boolean type
    Java boolean type conversion
    Convert string value to boolean
    Convert boolean to string

Char

    Java char type
    Compare two char values
    Change char case
    Java char conversion
    Java char value and its attributes

Byte

    Java byte type
    Convert Byte to String
    Convert String to byte
    Byte object constructor
    Byte's max and min value
    Compare two byte values
    Convert Byte to byte, double, float, int, long and short

Short

    Java short type
    Short min/max value and size
    Create Short object
    Compare short values
    Convert short to String
    Convert Short to primitive types
    Convert string to short
    Reverse bytes

Integer

    Java int type
    int max/min value
    Create Java integer
    Convert int to binary, hexadecimal and octal format
    Compare integer values
    Integer sign
    Convert string to int
    Convert int to primitive types
    Convert int to String
    int bit operations

Long

    Java long type
    Compare two long values
    Convert long to binary, hex and octal
    Convert long value to primitive types
    Convert String to long value
    Convert long to String

Float

    Java float type
    Java float type conversion
    Predefined value for float type
    Compare two float value

Double

    Java double type
    Deal with NaN double value
    Compare two double values
    Java double type creation and comparison
    Java double type conversion

Data Type Conversion

    Java Automatic Type Conversion and Casting
    Data type casting
    Java type promotion
    Autoboxing and auto-unboxing