Open In App

Java.Lang.Double Class in Java

Last Updated : 03 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Double class is a wrapper class for the primitive type double which contains several methods to effectively deal with a double value like converting it to a string representation, and vice-versa. An object of the Double class can hold a single double value. Double class is a wrapper class for the primitive type double which contains several methods to effectively deal with a double value like converting it to a string representation, and vice-versa. An object of the Double class can hold a single double value. 

There are mainly two constructors to initialize a Double-object.

A. Double(double b): Creates a Double-object initialized with the value provided where it takes a value with which to initialize as a parameter. 

public Double(double d) 

Parameters: Value with which to initialize

B. Double(String s): Creates a Double-object initialized with the parsed double value provided by string representation where it takes a string representation of the byte value as a parameter. 

Default radix is taken to be 10.  

public Double(String s) throws NumberFormatException

Exception Thrown: It throws NumberFormatException if the string provided does not represent any double value.

Methods of Double Class

Method Action Performed
byteValue() Returns a byte value corresponding to this Double Object
compare() Compare two primitive double values for numerical equality. As it is a static method therefore it can be used without creating any object of Double.
compareTo() Used to compare two Double objects for numerical equality and returns a value less than 0,0, a value greater than 0 for less than, equal to, and greater than.
doubleValue() Returns a double value corresponding to this Double Object.
doubleToLongBits() Returns the IEEE 754 floating-point “double format” bit layout of the given double argument. 
doubleToRawLongBits() Returns the IEEE 754 floating-point “double format” bit layout of the given double argument. It differs from the previous method as it preserves the Nan values.
equals() Compare the equality of two Double objects and returns true if both the objects contain same double value.
floatValue() Returns a float value corresponding to this Double Object.
hashCode() Returns the hashcode corresponding to this Double Object.
isInfinite() Returns true if the double object in consideration is very large, otherwise false. 
isNaN() Returns true if the double object in consideration is not a number, otherwise false.
intValue() Returns an integer value corresponding to this Double Object
longValue() Returns long value corresponding to this Double Object.
longBitsToDouble() Returns double value corresponding to the long bit pattern of the argument.
parseDouble() Returns double value by parsing the string.
shortValue() Returns short value corresponding to this Double Object
toHexString() Returns hexadecimal representation of the argument double value. 
toString() Returns the string corresponding to the double value
valueOf() Returns a Double-object initialized with the value provided
valueOf(String s) Returns a Double-object initialized with the value provided

Implementation:

Java




// Java Program to Illustrate Double Class
// Via Demonstrating Its Methods
 
// Class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Declaring and initializing
        // double and String values
        double b = 55.05;
        String bb = "45";
 
        // Construct two Double objects
        Double x = new Double(b);
        Double y = new Double(bb);
 
        // Method - toString()
        System.out.println("toString(b) = "
                           + Double.toString(b));
 
        // Method - valueOf()
        // Return Double object
        Double z = Double.valueOf(b);
        System.out.println("valueOf(b) = " + z);
        z = Double.valueOf(bb);
        System.out.println("ValueOf(bb) = " + z);
 
        // Method - parseDouble()
        // Return primitive double value
        double zz = Double.parseDouble(bb);
        System.out.println("parseDouble(bb) = " + zz);
 
        // Print statements
        System.out.println("bytevalue(x) = "
                           + x.byteValue());
        System.out.println("shortvalue(x) = "
                           + x.shortValue());
        System.out.println("intvalue(x) = " + x.intValue());
        System.out.println("longvalue(x) = "
                           + x.longValue());
        System.out.println("doublevalue(x) = "
                           + x.doubleValue());
        System.out.println("floatvalue(x) = "
                           + x.floatValue());
 
        int hash = x.hashCode();
        System.out.println("hashcode(x) = " + hash);
 
        boolean eq = x.equals(y);
        System.out.println("x.equals(y) = " + eq);
 
        int e = Double.compare(x, y);
        System.out.println("compare(x,y) = " + e);
 
        int f = x.compareTo(y);
        System.out.println("x.compareTo(y) = " + f);
 
        Double d = Double.valueOf("1010.54789654123654");
        System.out.println("isNaN(d) = " + d.isNaN());
 
        System.out.println("Double.isNaN(45.12452) = "
                           + Double.isNaN(45.12452));
 
        // Double.POSITIVE_INFINITY stores
        // the positive infinite value
        d = Double.valueOf(Double.POSITIVE_INFINITY + 1);
        System.out.println(
            "Double.isInfinite(d) = "
            + Double.isInfinite(d.doubleValue()));
 
        double dd = 10245.21452;
        System.out.println("Double.toString(dd) = "
                           + Double.toHexString(dd));
 
        long double_to_long = Double.doubleToLongBits(dd);
        System.out.println("Double.doubleToLongBits(dd) = "
                           + double_to_long);
 
        double long_to_double
            = Double.longBitsToDouble(double_to_long);
        System.out.println(
            "Double.LongBitsToDouble(double_to_long) = "
            + long_to_double);
    }
}


Output

toString(b) = 55.05
valueOf(b) = 55.05
ValueOf(bb) = 45.0
parseDouble(bb) = 45.0
bytevalue(x) = 55
shortvalue(x) = 55
intvalue(x) = 55
longvalue(x) = 55
doublevalue(x) = 55.05
floatvalue(x) = 55.05
hashcode(x) = 640540672
x.equals(y) = false
compare(x,y) = 1
x.compareTo(y) = 1
isNaN(d) = false
Double.isNaN(45.12452) = false
Double.isInfinite(d) = true
Double.toString(dd) = 0x1.4029b7564302bp13
Double.doubleToLongBits(dd) = 4666857980575363115
Double.LongBitsToDouble(double_to_long) = 10245.21452



Previous Article
Next Article

Similar Reads

Java.lang.Class class in Java | Set 1
Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It has no public constructor. Class objects are cons
31 min read
Java.lang.Class class in Java | Set 2
Java.lang.Class class in Java | Set 1 More methods: 1. int getModifiers() : This method returns the Java language modifiers for this class or interface, encoded in an integer. The modifiers consist of the Java Virtual Machine's constants for public, protected, private, final, static, abstract and interface. These modifiers are already decoded in Mo
18 min read
Java Guava | Doubles.indexOf(double[] array, double target) method with Examples
Doubles.indexOf(double[] array, double target) method of Guava's Doubles Class accepts two parameters array and target. If the target exists within the array, the method returns the position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(double[] array, double target)
3 min read
Java Guava | Doubles.indexOf(double[] array, double[] target) method with Examples
Doubles.indexOf(double[] array, double[] target) method of Guava's Doubles Class accepts two parameters array and target. If the target exists within the array, the method returns the start position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(double[] array, double
3 min read
Java.lang.StrictMath class in Java | Set 2
Java.lang.StrictMath Class in Java | Set 1More methods of java.lang.StrictMath class 13. exp() : java.lang.StrictMath.exp(double arg) method returns the Euler’s number raised to the power of double argument. Important cases: Result is NaN, if argument is NaN.Result is +ve infinity, if the argument is +ve infinity.Result is +ve zero, if argument is
6 min read
java.lang.instrument.ClassDefinition Class in Java
This class is used to bind together the supplied class and class file bytes in a single ClassDefinition object. These class provide methods to extract information about the type of class and class file bytes of an object. This class is a subclass of java.lang.Object class. Class declaration: public final class ClassDefinition extends ObjectConstruc
2 min read
java.lang.reflect.Proxy Class in Java
A proxy class is present in java.lang package. A proxy class has certain methods which are used for creating dynamic proxy classes and instances, and all the classes created by those methods act as subclasses for this proxy class. Class declaration: public class Proxy extends Object implements SerializableFields: protected InvocationHandler hIt han
4 min read
java.lang.MethodType Class in Java
MethodType is a Class that belongs to java.lang package. This class consists of various types of methods that help in most of cases to find the method type based on the input object, or parameters of the method. All the instances of methodType are immutable. This means the objects of the methodType class cannot be overridden by any other objects da
4 min read
java.lang.ref.WeakReference Class In Java
When we create an object in Java, an object isn't weak by default. To create a Weak Reference Object, we must explicitly specify this to the JVM. Why Weak Reference Objects are used: Unlike C/C++, Java supports Dynamic Garbage Collection. This is performed when the JVM runs the Garbage Collector. In order to not waste space, the garbage collector d
3 min read
java.lang.reflect.Parameter Class in Java
java.lang.reflect.Parameter class provides Information about method parameters, including their name and modifiers. It also provides an alternate means of obtaining attributes for the parameter. Some useful methods of Parameter class are: public int getModifiers(): It returns the modifier flags for the parameter represented by this Parameter object
4 min read
Practice Tags :