## Class Double in Java

Double class in java wraps up a value of primitive type double in an immutable Object. An Object of type Double contains a single field whose type is double.

The Double class provides an object wrapper for a double value. This is useful when you need to treat a double value as an object. For example, there are a number of utility methods that take a reference to an Object as one of their arguments. You cannot specify a double value for one of these arguments, but you can provide a reference to a Double-object that encapsulates the double value.

In Java, double values are represented using the IEEE 754 format. The Double class provides

constants for the three special values that are mandated by this format: POSITIVE_INFINITY,

NEGATIVE_INFINITY, and NaN (not-a-number).

The Double class also provides some utility methods, such as methods for determining whether a double value is an infinity value or NaN, for converting double values to other primitive types, and for converting a double to a String and vice versa.

valueOf() converts a string to a Double, doubleValue() returns the primitive double value of a Double object, and there are other methods for returning a Double value as a variety of other primitive types.

This class also provides some useful constants and static methods for testing double values. MIN_VALUE and MAX_VALUE are the smallest (closest to zero) and largest representable double values. isInfinite() in a class method and instance method forms test whether a double or a Double has an infinite value. Similarly, isNaN() tests whether a double or Double is not-a-number-this is a comparison that cannot be done directly because the NaN constant never tests equal to any other value, including itself. doubleToLongBits() and longBitsToDouble() allows us to manipulate the bit representation of a double directly.

Since the double values have a finite accuracy they can’t get arbitrarily close to zero. Also double can represent nonreal numbers. Like

Double.POSITIVE_INFINITY

Double.NaN

Double.NEGATIVE_INFINITY

Double can represent negative zero(-0.0). Dividing a positive number by negative zero gives NEGATIVE_INFINITY

Class Double in Java

If we divide a number by positive zero(0.0) we get POSITIVE_INFINITY

NaN stands for Not a Number(An undefined value) like-

- The square root of a negative number.
- zero or divide by zero.

### The structure of class Double is given below:

```
public final class java.lang.Double extends java.lang.Number{
//Member elements
public final static double MAX_VALUE;//MAX_VALUE=1.79769313486231570 e to the power +308
public final static double MIN_VALUE;//MIN_VALUE=4.94065645841246544 e to the power -324
public final static double NaN;//NAN is 0 divided by 0 . Not a number
public final static double NEGATIVE_INFINITY;//produced when a negative number is divided by 0 like -10/0
public final static double POSITIVE_INFINITY;//produced when a positive number is divided by 0 like 10/0
public static final Class TYPE;
//constructors
public Double(double value);//Constructs a newly allocated Double object that represents the primitive double argument.
public Double(String str);//Constructs a newly allocated Double object that represents the floating point value of double represented by String str.The string is converted to a double value by valueOf() method.This method may throw NumberFormatException if the string does not contain a parsable number.
//Methods:
public static native long doubleToLongBits(double value);
public static boolean isInfinite(double value);
public static boolean isNaN((double value);
public static native double longBitsToDouble((long bits);
public static String toString(double value);
public static Double valueOf(String str)throws NumberFormatException;
public double doubleValue();
public boolean equals(Object obj);
public float floatValue();
public int hashCode();
public int intValue();
public byte byteValue();
public boolean isInfinite();
public boolean isNaN();
public long longValue();
public String toString();
public short shortValue();
}
```

The details of the class structure are given as follows:

`public final static double MAX_VALUE;`

public final static double MAX_VALUE;//MAX_VALUE=1.79769313486231570 e to the power +30 is the largest value that can be represented by a double.

`public final static double MIN_VALUE;`

public final static double MIN_VALUE;//MIN_VALUE=4.94065645841246544 e to the power -32 is the smallest value that can be represented by a double.

`public final static double NaN;//NAN is 0 divided by 0 . Not a number`

public final static double NaN represents the value not-a-number (NaN), which is a special value produced by double operations such as division of zero by zero. When NaN is one of the operands, most arithmetic operations return NaN as the result.

Most comparison operators (<, <=, ==, >=, >) return false when one of their arguments is NaN. The exception is !=, which returns true when one of its arguments is NaN.

`public final static double NEGATIVE_INFINITY;`

public final static double NEGATIVE_INFINITY represents the value negative infinity, which is produced when a double operation underflows or a negative double value is divided by zero. Negative infinity is by definition less than any other double value.

`public final static double POSITIVE_INFINITY;`

public final static double POSITIVE_INFINITY represents the value positive infinity, which is produced when a double operation overflows or positive double value is divided by zero. Positive infinity is by definition greater than any other double value.

`public static final Class TYPE;`

public static final Class TYPE is the Class object that represents the type double. It is always true that Double.TYPE == double.class.

`public Double(double value);//Constructs a newly allocated Double object that represents the primitive double argument.`

public Double(double value); constructor constructs a newly allocated Double object that represents the primitive double argument method.

This constructor returns Creates a Double-object with the specified double value.**Parameter****value** – The double value to be encapsulated by this object.

`public Double(String str);//Constructs a newly allocated Double object that represents the floating-point value of double represented by String str. The string is converted to a double value by the valueOf() method. This method may throw NumberFormatException if the string does not contain a parsable number.`

public Double(String str); constructor constructs a newly allocated Double object that represents the floating-point value of double represented by String str. The string is converted to a double value by the valueOf() method.

This constructor Constructs a Double-object with the value specified by the given string. The string must contain a sequence of characters that forms a legal double literal.

This method may throw NumberFormatException if the string does not contain a parsable number method.

**Parameter****str**– -The string to be made into a Double-object.

`public static native long doubleToLongBits(double value);`

public static native long doubleToLongBits(double value) method returns the long value that contains the same sequence of bits as the representation of the given double value. The meaning of the bits in the result is defined by the IEEE 754 floating-point format: bit 63 is the sign bit, bits 62-52 are the exponent, and bits 51-0 are the mantissa.

An argument of POSITIVE_INFINITY produces the result 0x7ff0000000000000L, an argument of NEGATIVE_INFINITY produces the result 0xfff0000000000000L, and an

argument of NaN produces the result 0x7ff8000000000000L.

The value returned by this method can be converted back to the original double value by the

longBitsToDouble() method.

This method returns the long value that contains the same sequence of bits as the representation of the given double value.**Parameter****value** – The double value to be converted.

`public static boolean isInfinite(double value);`

public static boolean isInfinite(double value) method determines whether or not the specified value is an infinity value.

This method returns true if the specified value is equal to POSITIVE_INFINITY or NEGATIVE_INFINITY; otherwise false.

**Parameter****value** – The double value to be tested.

`public static boolean isNaN((double value);`

public static boolean isNaN((double value) method determines whether or not the specified value is NaN.

This method returns true if the specified value is equal to NaN; otherwise false.**Parameter****value** – The double value to be tested.

`public static native double longBitsToDouble((long bits);`

public static native double longBitsToDouble((long bits) method returns the double value whose representation is the same as the bits in the given double value. The meaning of the bits in the long value is defined by the IEEE 754 floating-point format: bit 63 is the sign bit, bits 62-52 are the exponent, and bits 51-0 are the mantissa. The argument 0x7f80000000000000L produces the result POSITIVE_INFINITY and the argument 0xff80000000000000L produces the result NEGATIVE_INFINITY.

Arguments in the ranges 0x7ff0000000000001L through 0x7fffffffffffffffL and

0xfff0000000000001L through 0xffffffffffffffffL all produce the result NaN.

Except for NaN values not normally used by Java, this method is the inverse of the

doubleToLongBits() method.

This method returns the double value whose representation is the same as the bits in the given long value.

**Parameter****bits**– The long value to be converted.

`public static String toString(double value);`

public static String toString(double value) method returns a String object that contains a representation of the given double value.

The values NaN, NEGATIVE_INFINITY, POSITIVE_INFINITY, -0.0, and +0.0 are represented by the strings “NaN”, “-Infinity”, “Infinity”, “-0.0”, and “0.0”,respectively.

For other values, the exact string representation depends on the value being converted. If the absolute value of d is greater than or equal to 10^-3 or less than or equal to 10^7, it is converted to a string with an optional minus sign (if the value is negative) followed by up to eight digits before the decimal point, a decimal point, and the necessary number of digits after the decimal point (but no trailing zero if there is more than one significant digit). There is always a minimum of one digit after the decimal point.

Otherwise, the value is converted to a string with an optional minus sign (if the value is negative), followed by a single digit, a decimal point, the necessary number of digits after the decimal point (but no trailing zero if there is more than one significant digit), and the letter E followed by a plus or a minus sign and a base 10 exponent of at least one digit. Again, there is always a minimum of one digit after the decimal point.

This method returns a string representation of the given value.**Parameter****value** – The double value to be converted.

`public static Double valueOf(String str)throws NumberFormatException;`

public static Double valueOf(String str)throws NumberFormatException method Constructs a Double-object with the value specified by the given string. The string must contain a

sequence of characters that forms a legal double literal. This method ignores leading and trailing white space in the string.

This method returns the Double-object constructed from the string.**Parameter****str** – The string to be made into a Double-object.

`public double doubleValue();`

public double doubleValue() method returns the value of this object as a double.

This method returns the value of this object as a double.

`public boolean equals(Object obj);`

public boolean equals(Object obj) method returns true if obj is an instance of Double and it contains the same value as the object this method is associated with. More specifically, the method returns true if the doubleToLongBits() method returns the same result for the values of both objects.

This method produces a different result than the == operator when both values are NaN. In this case, the == operator produces false, while this method returns true. By the same token, the method also produces a different result when the two values are +0.0 and -0.0. In this case, the == operator produces true, while this method returns false.

This method returns true if the objects are equal; false if they are not.**Parameter****obj** – The object to be compared with this object.

`public float floatValue();`

public float floatValue() method returns this object value as a float. Rounding may occur.

This method returns the value of this object as a float.

`public int hashCode();`

public int hashCode() method returns a hashcode computed from the value of this object. More specifically, if d is the value of the object, and bitValue is defined as:

`long bitValue = Double.doubleToLongBits(d)`

This method returns a hashcode based on the double value of the object.

then the hashcode returned by this method is computed as follows:

`(int)(bitValue ^ (bitValue>>>32))`

`public int intValue();`

public int intValue() method returns the truncated value of this object as an int. More specifically, if the value of the object is NaN, the method returns 0. If the value is POSITIVE_INFINITY or any other value that is too large to be represented by an int, the method returns Integer.MAX_VALUE.

If the value is NEGATIVE_INFINITY, or any other value that is too small to be represented by

an int, the method returns Integer.MIN_VALUE. Otherwise, the value is rounded toward zero

and returned.

This method returns the value of this object as an int.

`public byte byteValue();`

public byte byteValue() method returns the truncated value of this object as a byte. More specifically, if the value of the object is NaN, the method returns 0. If the value is POSITIVE_INFINITY or any other value that is too large to be represented by a byte, the method returns Byte.MAX_VALUE.

If the value is NEGATIVE_INFINITY or any other value that is too small to be represented by a byte, the method returns Byte.MIN_VALUE. Otherwise, the value is rounded toward zero and returned.

This method returns the value of this object as a byte.

`public boolean isInfinite();`

public boolean isInfinite() method determines whether or not the value of this object is an infinity value.

This method returns true if the value of this object is equal to POSITIVE_INFINITY or NEGATIVE_INFINITY; otherwise false.

`public boolean isNaN();`

public boolean isNaN() method determines whether or not the value of this object is NaN.

This method returns true if the value of this object is equal to NaN; otherwise false.

`public long longValue();`

public long longValue() method returns the truncated value of this object as a long. More specifically, if the value of the object is NaN, the method returns 0.

If the value is POSITIVE_INFINITY, or any other value too large to be represented by a long, the method returns Long.MAX_VALUE. If the value is NEGATIVE_INFINITY, or any other value too small to be represented by a long, the method returns Long.MIN_VALUE. Otherwise, the value is rounded toward zero and returned.

This method returns the value of this object as a long.

`public String toString();`

public String toString() method returns a String object that contains a representation of the value of this object.

The values NaN, NEGATIVE_INFINITY, POSITIVE_INFINITY, -0.0, and +0.0 are represented by the strings “NaN”, “-Infinity”, “Infinity”, “-0.0”, and “0.0”, respectively.

For other values, the exact string representation depends on the value being converted. If the absolute value of this object is greater than or equal to 10^-3 or less than or equal to 10^7, it is converted to a string with an optional minus sign (if the value is negative) followed by up to eight digits before the decimal point, a decimal point, and the necessary number of digits after the decimal point (but no trailing zero if there is more than one significant digit). There is always a minimum of one digit after the decimal point.

Otherwise, the value is converted to a string with an optional minus sign (if the value is negative), followed by a single digit, a decimal point, the necessary number of digits after the decimal point (but no trailing zero if there is more than one significant digit), and the letter E followed by a plus or a minus sign and a base 10 exponent of at least one digit. Again, there is always a minimum of one digit after the decimal point.

This method returns a string representation of the value of this object.

`public short shortValue();`

public short shortValue() method returns the truncated value of this object as a short. More specifically, if the value of the object is NaN, the method returns 0. If the value is POSITIVE_INFINITY or any other value that is too large to be represented by a short, the method returns Short.MAX_VALUE.

If the value is NEGATIVE_INFINITY or any other value that is too small to be represented by

a short, the method returns Short.MIN_VALUE. Otherwise, the value is rounded toward zero

and returned.

This method returns the value of this object as a short.

Apart from these Double class also has inherited methods from class- Object. They are as follows:

- clone()
- finalize()
- hashCode()
- notifyAll()
- wait()
- wait(long, int)
- equals(Object)
- getClass()
- notify()
- toString()
- wait(long)

class double

We can also test if a value ‘x’ is of the type double or is infinity or undefined by calling the static methods:

```
Double.isInfinite(x);
Double.isNaN(x);
```

class double

These method returns boolean value to indicate if the given value is Infinite or NaN.

```
x==Double.NaN;
x!=Double.NaN;
```

Both will return false.

Math.random() function returns a double value.

we need to work with range to trim to our required value.

```
int max=6;
int min=1;
int range=max-min+1;
int rand=(int)(Math.random()*range+min);
```