Showing posts with label Double Class. Show all posts
Showing posts with label Double Class. Show all posts

Double class Methods in Java

java.lang.Double

The Double class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double. In addition, this class provides several methods for converting a double to a String and a String to a double, as well as other constants and methods useful when dealing with a double.

Some Methods of Double Class

Double.BYTESThe number of bytes used to represent a double value (i.e 8 is returned).


Double.compare(): It compares the two specified double values.


Double.doubleToLongBits() It returns a representation of the specified floating-point value according to the IEEE 754 floating-point "double format" bit layout.


Double.doubleToRawLongBits()It returns a representation of the specified floating-point value according to the IEEE 754 floating-point "double format" bit layout, preserving Not-a-Number (NaN) values.


Double.hashCode()It returns a hash code for a double value.


Double.isFinite()It returns true if the argument is a finite floating-point value; returns false otherwise (for NaN and infinity arguments).


Double.isInfinite() It returns true if the specified number is infinitely large in magnitude, false otherwise.


Double.isNaN()It returns true if the specified number is Not-a-Number (NaN) value, false otherwise.


Double.longBitsToDouble()It returns the double value corresponding to a given bit representation. The argument is considered to be a representation of a floating-point value according to the IEEE 754 floating-point"double format" bit layout.


Double.max(): It returns the greater of two double values.


Double.MAX_EXPONENTMaximum exponent a finite double variable may have (i.e 1023).


Double.MAX_VALUEA constant holding the largest positive finite value of type double (i.e. 1.7976931348623157E308)


Double.min()It returns the smaller of two double values.


Double.MIN_EXPONENTMinimum exponent a normalized double variable may have (i.e. -1022)


Double.MIN_NORMAL A constant holding the smallest positive normal value of type double (i.e. 2.2250738585072014E-308)


Double.NaNA constant holding a Not-a-Number (NaN) value of type double.


Double.NEGATIVE_INFINITYA constant holding the negative infinity of type double.


Double.parseDouble()It returns a new double initialized to the value represented by the specified String.


Double.POSITIVE_INFINITYA constant holding the positive infinity of type double.


Double.SIZEThe number of bits is used to represent a double value (i.e. 64).


Double.sum()It adds two double values together as per the + operator.


Double.toHexString()It returns a hexadecimal string representation of the double argument.


Double.toString()It returns a string representation of the double argument.


Double.valueOf(double d): It returns a Double instance representing the specified double value.


Double.valueOf()It returns a Double-object holding the double value represented by the argument string s.


Double.valueOf() String in Java

Double.valueOf(): This method is available in java.lang.Double class of Java.

Syntax:

Double java.lang.Double.valueOf(String s) throws NumberFormatException

This method takes one argument of type String as its parameter. This method returns a Double object holding the double value represented by the argument string s.

Note: If s is null, then a NullPointerException is thrown.

Parameters: One parameter is required for this method.

s: the string to be parsed.

Returns: a Double-object holding the value represented by the String argument.

Throws: NumberFormatException - if the string does not contain a parsable number.

For Example:

String s = "1818.56"

Double.valueOf(s) = > It returns 1818.56

Approach

Java

public class DoubleValueofString {
    public static void main(String[] args) {

        String s = "1818.56";
        System.out.println(Double.valueOf(s));
    }
}

Output:

1818.56

Double.valueOf() double in Java

Double.valueOf(): This method is available in java.lang.Double class of Java.

Syntax:

Double java.lang.Double.valueOf(double d)

This method takes one argument of type double as its parameter. This method returns a Double instance representing the specified double value.

Parameters: One parameter is required for this method.

d: a double value.

Returns: a Double instance representing d.

For Example:

double d = 188.7

Double.valueOf(d) = > It returns 188.7

Approach

Java

public class DoubleValueofDouble {
    public static void main(String[] args) {

        double d = 188.7;
        System.out.println(Double.valueOf(d));

    }
}

Output:

188.7

Double.toString() in Java

Double.toString(): This method is available in java.lang.Double class of Java.

Syntax:

String java.lang.Double.toString(double d)

This method takes one argument of type double as its parameter. This method returns a string representation of the double argument.

Note:

1. If the argument is NaN, the result is the string"NaN".

2. Otherwise, the result is a string that represents the sign and magnitude (absolute value) of the argument.

Parameters: One parameter is required for this method.

d: the double to be converted.

Returns: a string representation of the argument.

For Example:

double d = 1919.5

Double.toString(d) = > It returns 1919.5

Approach

Java

public class DoubletoString {
    public static void main(String[] args) {

        double d = 1919.5;
        System.out.println(Double.toString(d));

    }
}

Output:

1919.5

Double.toHexString() in Java

Double.toHexString(): This method is available in java.lang.Double class of Java.

Syntax:

String java.lang.Double.toHexString(double d)

This method takes one argument of type double as its parameter. This method returns a hexadecimal string representation of the double argument.

Note: 

1. If the argument is NaN, the result is the string"NaN".

2. Otherwise, the result is a string that represents the sign and magnitude of the argument.

Parameters: One parameter is required for this method.

d: the double to be converted.

Returns: a hex string representation of the argument.

For Example:

double d = 19919.6

Double.toHexString(d) = > It returns 0x1.373e666666666p14

Approach

Java

public class DoubletoHexString {
    public static void main(String[] args) {

        double d = 19919.6;
        System.out.println(Double.toHexString(d));

    }
}

Output:

0x1.373e666666666p14

Double.sum() in Java

Double.sum(): This method is available in java.lang.Double class of java.

Syntax:

double java.lang.Double.sum(double a, double b)

This method takes two arguments of type double as its parameters. This method adds two double values together as per the + operator.

Parameters: Two parameters are required for this method.

a: the first operand.

b: the second operand.

Returns: The sum of a and b.

For Example:

double a = 1919.6, b = 9891.7

Double.sum(a,b) = > It returns 11811.300000000001

Approach

Java

public class DoubleSum {
    public static void main(String[] args) {

        double a = 1919.6, b = 9891.7;
        System.out.println(Double.sum(a, b));

    }
}

Output:

11811.300000000001

Double.SIZE in Java

Double.SIZE: This is available in java.lang.Double class of Java.

Syntax:

int java.lang.Double.SIZE

The number of bits is used to represent a double value (i.e. 64).

Approach

Java

public class DoubleSize {
    public static void main(String[] args) {

        System.out.println(Double.SIZE);

    }
}

Output:

64

Double.POSITIVE_INFINITY in Java

Double.POSITIVE_INFINITY: This is available in java.lang.Double class of Java.

Syntax:

double java.lang.Double.POSITIVE_INFINITY

A constant holding the positive infinity of type double.

Approach

Java

public class DoublePositiveInfinity {
    public static void main(String[] args) {

        System.out.println(Double.POSITIVE_INFINITY);

    }
}

Output:

Infinity

Double.parseDouble() in Java

Double.parseDouble(): This method is available in java.lang.Double class of Java.

Syntax:

double java.lang.Double.parseDouble(String s) throws NumberFormatException

This method takes one argument of type String as its parameter. This method returns a new double initialized to the value represented by the specified String.

Parameters: One parameter is required for this method.

s: the string to be parsed.

Returns: the double value represented by the string argument.

Throws: NullPointerException - if the string is nullNumberFormatException - if the string does not contain a parsable double.

For Example:

String s = "1818.99"

Double.parseDouble(s) = > It returns 1818.99

Approach

Java

public class DoubleparseDouble {
    public static void main(String[] args) {

        String s = "1818.99";
        System.out.println(Double.parseDouble(s));

    }
}

Output:

1818.99

Double.NEGATIVE_INFINITY in Java

Double.NEGATIVE_INFINITY: This is available in java.lang.Double class of Java.

Syntax:

double java.lang.Double.NEGATIVE_INFINITY 

A constant holding the negative infinity of type double.

Approach

Java

public class DoubleNegativeInfinity {
    public static void main(String[] args) {

        System.out.println(Double.NEGATIVE_INFINITY);

    }
}

Output:

-Infinity

Double.NaN in Java

Double.NaN: This is available in java.lang.Double class of Java.

Syntax:

double java.lang.Double.NaN 

A constant holding a Not-a-Number (NaN) value of type double.

Approach

Java

public class DoubleNaN {
    public static void main(String[] args) {

        System.out.println(Double.NaN);

    }
}

Output:

NaN

Double.MIN_NORMAL in Java

Double.MIN_NORMAL: This is available in java.lang.Double class of Java.

Syntax:

double java.lang.Double.MIN_NORMAL

 A constant holding the smallest positive normal value of type double (i.e. 2.2250738585072014E-308)

Approach

Java

public class DoubleMinNormal {
    public static void main(String[] args) {

        System.out.println(Double.MIN_NORMAL);

    }
}

Output:

2.2250738585072014E-308

Double.MIN_EXPONENT in Java

Double.MIN_EXPONENT: This method is available in java.lang.Double class of Java.

Syntax:

int java.lang.Double.MIN_EXPONENT

Minimum exponent a normalized double variable may have (i.e. -1022)

Approach

Java

public class DoubleMinExponent {
    public static void main(String[] args) {

        System.out.println(Double.MIN_EXPONENT);

    }
}

Output:

-1022

Double.min() in Java

Double.min(): This method is available in java.lang.Double class of Java.

Syntax:

double java.lang.Double.min(double a, double b)

This method takes two arguments of type double as its parameters. This method returns the smaller of two double values.

Parameters: Two parameters are required for this method.

a: the first operand.

b: the second operand.

Returns: the smaller of a and b.

For Example:

double a = 1919.56, b = 1991.5

Double.min(a,b) = > It returns 1919.56

Approach

Java

public class DoubleMin {
    public static void main(String[] args) {

        double a = 1919.56, b = 1991.5;
        System.out.println(Double.min(a, b));

    }
}

Output:

1919.56

Double.MAX_VALUE in Java

Double.MAX_VALUE: This is available in java.lang.Double class of Java.

Syntax:

double java.lang.Double.MAX_VALUE

A constant holding the largest positive finite value of type double (i.e. 1.7976931348623157E308)

Note:

It is equal to the hexadecimal floating-point literal 0x1.fffffffffffffP+1023 and also equal to Double.longBitsToDouble(0x7fefffffffffffffL).

Approach

Java

public class DoubleMaxValue {
    public static void main(String[] args) {

        System.out.println(Double.MAX_VALUE);

    }
}

Output:

1.7976931348623157E308

Double.MAX_EXPONENT in Java

Double.MAX_EXPONENT: This is available in java.lang.Double class of Java.

Syntax:

int java.lang.Double.MAX_EXPONENT

Maximum exponent a finite double variable may have (i.e 1023).

Approach

Java

public class DoubleMaxExponent {
    public static void main(String[] args) {

        System.out.println(Double.MAX_EXPONENT);

    }
}

Output:

1023

Double.max() in Java

Double.max(): This method is available in java.lang.Double class of Java.

Syntax:

double java.lang.Double.max(double a, double b)

This method takes two arguments of type double as its parameter. This method returns the greater of two double values.

Parameters: Two parameters are required for this method.

a: the first operand.

b: the second operand.

Returns: the greater of a and b.

For Example:

double a = 1991.5, b = 1991.119

Double.max(a,b) = > It returns 1991.5

Approach

Java

public class DoubleMax {
    public static void main(String[] args) {

        double a = 1991.5, b = 1991.119;
        System.out.println(Double.max(a, b));

    }
}

Output:

1991.5

Double.longBitsToDouble() in Java

Double.longBitsToDouble(): This method is available in java.lang.Double class of Java.

Syntax:

double java.lang.Double.longBitsToDouble(long bits)

This method takes one argument of type long as its parameter. This method returns the double value corresponding to a given bit representation. The argument is considered to be a representation of a floating-point value according to the IEEE 754 floating-point"double format" bit layout.

Note:

1. If the argument is 0x7ff0000000000000L, the result is positive infinity.

2. If the argument is 0xfff0000000000000L, the result is negative infinity.

3. If the argument is any value in the range 0x7ff0000000000001L through 0x7fffffffffffffffL or in the range 0xfff0000000000001L through 0xffffffffffffffffL, the result is a NaN.

Parameters: bits any long integer.

Returns: the double floating-point value with the same bit pattern.

For Example:

long bits = 18818

Double.longBitsToDouble(bits) = > It returns 9.2973E-320.

Approach

Java

public class DoublelongBitsToDouble {
    public static void main(String[] args) {

        long bits = 18818;
        System.out.println(Double.longBitsToDouble(bits));

    }
}

Output:

9.2973E-320

Double.isNaN() in Java

Double.isNaN(): This method is available in java.lang.Double class of Java.

Syntax:

boolean java.lang.Double.isNaN(double v)

This method takes one argument of type double as its parameter. This method returns true if the specified number is Not-a-Number (NaN) value, false otherwise.

Parameters: One parameter is required for this method.

v: the value to be tested.

Returns: true if the value of The argument is NaN; false otherwise.

For Example:

double v = 177.9

Double.isNaN(v) = > It returns false.

Approach

Java

public class DoubleisNaN {
    public static void main(String[] args) {

        double v = 177.9;
        System.out.println(Double.isNaN(v));

    }
}

Output:

false

Double.isInfinite() in Java

Double.isInfinite(): This method is available in java.lang.Double class of Java.

Syntax:

boolean java.lang.Double.isInfinite(double v)

This method takes one argument of type double as its parameter. This method returns true if the specified number is infinitely large in magnitude, false otherwise.

Parameters: One parameter is required for this method.

v: the value to be tested.

Returns: true if the value of the argument is positive infinity or negative infinity; false otherwise.

For Example:

double v = 1919.9

Double.isInfinite(v) = > It returns false.

Approach

Java

public class DoubleisInfinite {
    public static void main(String[] args) {

        double v = 1919.9;
        System.out.println(Double.isInfinite(v));

    }
}

Output:

false