Table of contents
1.
Introduction
2.
Java Double Class
3.
Double Class Declaration
4.
Double Class Basic Implementation
4.1.
Output
5.
Fields of Double Class
6.
Constructors of Double Class
7.
Methods of Double Class
7.1.
1. toString()
7.1.1.
Syntax
7.1.2.
Example
7.1.3.
Output
7.2.
2. valueOf()
7.2.1.
Syntax
7.2.2.
Example
7.2.3.
Output
7.3.
3. parseDouble()
7.3.1.
Syntax
7.3.2.
Example
7.3.3.
Output
7.4.
4. byteValue()
7.4.1.
Syntax
7.4.2.
Example
7.4.3.
Output
7.5.
5. shortValue()
7.5.1.
Syntax
7.5.2.
Example
7.5.3.
Output
7.6.
6. intValue()
7.6.1.
Syntax
7.7.
7. longValue()
7.7.1.
Syntax
7.8.
8. doubleValue()
7.8.1.
Syntax
7.9.
9. floatValue()
7.9.1.
Syntax
7.9.2.
Example
7.9.3.
Output
7.10.
10. hashCode()
7.10.1.
Syntax
7.10.2.
Example
7.10.3.
Output
7.11.
11. isNaN()
7.11.1.
Syntax
7.11.2.
Example
7.11.3.
Output
7.12.
12. isInfinite()
7.12.1.
Syntax
7.12.2.
Example
7.12.3.
Output
7.13.
13. toHexString()
7.13.1.
Syntax
7.13.2.
Example
7.13.3.
Output
7.14.
14. doubleToLongBits()
7.14.1.
Syntax
7.14.2.
Example
7.14.3.
Output
7.15.
15. doubleToRawLongBits()
7.15.1.
Syntax
7.15.2.
Example
7.15.3.
Output
7.16.
16. LongBitsToDouble()
7.16.1.
Syntax
7.16.2.
Example
7.16.3.
Output
7.17.
17. equals()
7.17.1.
Syntax
7.17.2.
Example
7.17.3.
Output
7.18.
18. compareTo()
7.18.1.
Syntax
7.18.2.
Example
7.18.3.
Output
7.19.
19. compare()
7.19.1.
Syntax
7.19.2.
Example
7.19.3.
Output
8.
FAQs
9.
Key Takeaways
Last Updated: Mar 27, 2024

Java Double Class

Introduction

Have you ever heard about “packages” and “classes” in Java? If you are curious to get a basic idea about them, the article “Java Long Class” will give you an understanding of the basic concepts of Java Classes and Packages.

In this post, we will discuss the declaration of Double class, which is a built-in Java Class of “Java.Lang” Package. We will also learn various fieldsconstructors and methods available in the Double class.

I hope, when you reach the end of this post, you will have a deep understanding of the basic concepts of Java Double Class. Let’s get started.

Also see, Duck Number in Java  and  Swap Function in Java

Java Double Class

The Java Double class is a wrapper class. In simple words, it contains the primitive data type “double” in its object. Of course, this object is generated from the Java Double Class. A single field of type double is contained in a “Double” object.

Additionally, this class provides useful methods for dealing with double variables, for instance, we can convert a double value to a String and a String to a double value.

Let’s learn different constructors, methods and fields available in Java Double Class.

Double Class Declaration

In this section, we'll look at how a Long class is implemented in Java. The following is an example of a Long class declaration code:

public final class Double extends Number
implements Comparable<Double>
You can also try this code with Online Java Compiler
Run Code

Double Class Basic Implementation

You must be interested in learning how to implement our first Double class in Java. We've included a basic example of how to use the Double class below.

//java double class example usage
import java.lang.Double;
public class DoubleClassDeclaration {
    public static void main(String[] args) {
        Double d = new Double(12345d);
        System.out.println(d);
        //hashcode generation using hashCode() method
        System.out.println("HashCode for the double value '"+d+"' is :"+ d.hashCode());
        //interger type conversion method
        System.out.println("The interger type value for the double '"+d+"' is :"+d.intValue());
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

In the above example, we have initialised a double variable and converted it to its respective hashcode using the hashCode() method. We have also shown its respective integer value using intValue() method.

Fields of Double Class

We define a “field” as a data container that holds data. It can contain the class’s descriptive properties. Let’s see various fields and their properties of the Double Class.

The Java.Lang.Double Class has the following fields associated with it:

Constructors of Double Class

For creating objects of the Double class, we have two types of constructors. One constructor takes a double value as a parameter, and the other takes a String.

  • Double(double value): creates a newly allocated Double object representing the primitive double argument.
  • Double(String s): creates a new Double-object representing the floating-point value of type double represented by the string.

Methods of Double Class

We will now learn various methods of Double class and their examples. Various methods are available in the Java Double class, which are very useful to the programmer. Below, we have listed the methods with examples.

1. toString()

We use the toString() method to return a new string representing the given double object. Let’s make it clearer by looking at the syntax and example below.

Syntax

public String toString(double d)
You can also try this code with Online Java Compiler
Run Code

Example

//java double class toString() method
import java.lang.Double;
public class DoubleClass
{
    public static void main(String[] args)
    {
        double xyz = 65.35;
        System.out.println("Converting to string = " + Double.toString(xyz));
     }
}
You can also try this code with Online Java Compiler
Run Code

Output

2. valueOf()

This method returns the Double instance that represents the specified double value. Below is the syntax and example of the implementation of this method.

Syntax

public static Double valueOf(double d)
You can also try this code with Online Java Compiler
Run Code

Example

//java double class valueOf() method
import java.lang.Double;
import java.lang.String;
public class DoubleClass
{
    public static void main(String[] args)
    {
        String s = "95";
        double d1 = Double.valueOf("123.45");
        System.out.println("value of(d1) = " + d1);
        d1 = Double.valueOf(s);
        System.out.println("value of(s) = " + d1);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

3. parseDouble()

We use this method to convert the provided string value and return it as a double value. It only accepts one String type argument. The syntax and example of this method is given below.

Syntax

public static double parseDouble(String val) throws NumberFormatException
You can also try this code with Online Java Compiler
Run Code

Example

//java double class parseDouble() method
import java.lang.Double;
import java.lang.String;
public class DoubleClass
{
    public static void main(String[] args)
    {
        String s = "123";
        double d = Double.parseDouble(s);
        System.out.println("Parse Double (string s) = " +d);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

4. byteValue()

After the conversion, it returns the value of Double as a byte. The syntax is given below and then go through the example to get a more precise understanding of this method.

Syntax

public byte byteValue()
You can also try this code with Online Java Compiler
Run Code

Example

//java double class byteValue() method
import java.lang.Double;
public class DoubleClass {
    public static void main(String[] args) {
        double d = new Double(1.23456789).byteValue();
        System.out.println(d);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

5. shortValue()

After the conversion, it returns the value of Double as a short. Below is the syntax followed by an example of the implementation of the shortValue() method.

Syntax

public short shortValue()
You can also try this code with Online Java Compiler
Run Code

Example

//java double class shortValue() method
import java.lang.Double;
public class DoubleClass {
    public static void main(String[] args) {
        double d = new Double(3.14).shortValue();
        System.out.println(d);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

6. intValue()

After the conversion, it returns the value of Double as an int. You can implement it in the same way as other methods described above. For better understanding, an example describing this method is given floatValue() method below.

Syntax

public int intValue()
You can also try this code with Online Java Compiler
Run Code

7. longValue()

After the conversion, it returns the value of Double as a long. We can implement in the same way as the above programs of other methods like shortValue(). We have given an example describing this method in floatValue() method below. Below is the syntax of longValue() method.

Syntax

public int longValue()
You can also try this code with Online Java Compiler
Run Code

8. doubleValue()

After a widening primitive conversion, it returns the value of this Double type as a double type. Below is the syntax. An example of implementation of this method is given in the floatValue() method below.

Syntax

public int doubleValue()
You can also try this code with Online Java Compiler
Run Code

9. floatValue()

For the given Double-object, it returns the float type value. We can implement in the same way as the above programs of other methods like shortValue(). Below is the syntax of the longValue() method.

Syntax

public int floatValue()
You can also try this code with Online Java Compiler
Run Code

Example

//java double class floatValue(), intValue(), longValue(), doubleValue() method
import java.lang.Double;
public class DoubleClass {
    public static void main(String[] args)
    {
        double a = 123.45;
        Double obj1 = new Double(a);
        //printing the value of a
        System.out.println("intvalue(obj) = " + obj1.intValue());
        System.out.println("longvalue(obj) = " + obj1.longValue());
        System.out.println("doublevalue(obj) = " + obj1.doubleValue());
        System.out.println("floatvalue(obj) = " + obj1.floatValue());
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

10. hashCode()

This method gives the hash code of a value of the double type. It returns an int value. Below is the syntax and example of it.

Syntax

public int hashCode()
You can also try this code with Online Java Compiler
Run Code

Example

//java double class hashCode() method
import java.lang.Double;
public class DoubleClass {
    public static void main(String[] args)
    {
        double a = 123.45;
        Double obj1 = new Double(a);
        System.out.println(obj1.hashCode());
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

11. isNaN()

If the given number is not a number, it returns true. Otherwise, it returns a false value. Below is the syntax and a sample implementation.

Syntax

public boolean isNaN()
You can also try this code with Online Java Compiler
Run Code

Example

//java double class isNaN() method
import java.lang.Double;
public class DoubleClass {
    public static void main(String[] args)
    {
        double a = 123.45;
        Double obj1 = new Double(a);
        System.out.println(obj1.isNaN());
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

12. isInfinite()

If the given number is infinitely large in magnitude, it returns true. Otherwise, it returns false. Below is the syntax and example.

Syntax

public boolean isInfinite()
You can also try this code with Online Java Compiler
Run Code

Example

//java double class isInfinite() method
import java.lang.Double;
public class DoubleClass {
    public static void main(String[] args)
    {
        double a = 123.45;
        Double obj1 = new Double(a);
        System.out.println("Is a finite number? " + obj1.isInfinite());
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

13. toHexString()

It returns the double argument as a hexadecimal string. Below is the syntax and example.

Syntax

public static String toHexString(double val)
You can also try this code with Online Java Compiler
Run Code

Example

//java double class toHexString() method
import java.lang.Double;
public class DoubleClass {
    public static void main(String[] args)
    {
        double a = 123.45;
        Double obj1 = new Double(a);
        Double obj2 = Double.valueOf(a);
        System.out.println(Double.toHexString(a));
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

14. doubleToLongBits()

The Java Double class's doubleToLongBits() method returns a floating-point value in IEEE 754 floating-point "double format" bit layout.

Syntax

public static long doubleToLongBits(double val)
You can also try this code with Online Java Compiler
Run Code

Example

//java double class doubleToLongBits() method
import java.lang.Double;
public class DoubleClass {
    public static void main(String[] args)
    {
        double a = 123.45;
        Double obj1 = new Double(a);
        long b = obj1.doubleToLongBits(a);
        System.out.println("double to long bits: " + b);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

15. doubleToRawLongBits()

It returns the IEEE 754 "double format" bit layout's representation of the floating-point value. Additionally, it preserves the NaN (Not a Number) value. Let’s see the syntax and example given below.

Syntax

public static long doubleToRawLongBits(double val)
You can also try this code with Online Java Compiler
Run Code

Example

//java double class doubleToRawLongBits() method
import java.lang.Double;
public class DoubleClass {
    public static void main(String[] args)
    {
        double a = 123.45;
        Double obj1 = new Double(a);
        long b = obj1.doubleToRawLongBits(a);
        System.out.println("double to raw long bits: " + b);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

16. LongBitsToDouble()

It returns a Double value that corresponds to the given bit representation. Let’s see the syntax and example given below.

Syntax

public static double LongBitsToDouble(long val)
You can also try this code with Online Java Compiler
Run Code

Example

//java double class longBitsToDouble() method
import java.lang.Double;
public class DoubleClass {
    public static void main(String[] args)
    {
        double d = Double.longBitsToDouble(4638387438405602509L);
        System.out.println(d);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

17. equals()

When two Double objects are compared for equality, this function is used. If both objects have the same double value, this function returns true. Let’s see the syntax and example given below.

Syntax

public boolean equals(Object obj1)
You can also try this code with Online Java Compiler
Run Code

Example

//java double class equals() method
import java.lang.Double;
public class DoubleClass {
    public static void main(String[] args)
    {
        double a = 123.45;
        String s ="12";
        Double obj1 = new Double(a);
        Double obj2 = new Double(s);
        System.out.println(obj1.equals(obj2));
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

18. compareTo()

This method is used when two Double objects are compared for numerical equality. This should be used when comparing two Double values for numerical equality because it distinguishes between smaller and greater values. 

→ It returns 0 if both objects are equal.

→ Returns less than 0 if one double object is less than the argument.

→ Returns more than 0otherwise.

Let’s see the syntax and example given below.

Syntax

public int compareTo(Double b)
You can also try this code with Online Java Compiler
Run Code

Example

//java double class equals() method
import java.lang.Double;
public class DoubleClass {
    public static void main(String[] args)
    {
        double a = 123.45;
        String s ="12";
        Double obj1 = new Double(a);
        Double obj2 = new Double(s);
        System.out.println(obj1.equals(obj2));
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

Try it on java online compiler.

19. compare()

This function is used when two primitive double values are compared for numerical equality. Because it is a static method, we can use it without generating a Double-object.

Let’s see the syntax and example given below.

Syntax

public int compare(double a, double b)
You can also try this code with Online Java Compiler
Run Code

Example

//java double class compare() method
import java.lang.Double;
public class DoubleClass {
    public static void main(String[] args)
    {
        double a = 123.45;
        String b = "12";
        Double obj1 = new Double(a);
        Double obj2 = new Double(b);
        int x = Double.compare(obj1,obj2);
        System.out.println(x);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

FAQs

  1. Is it possible to have a negative double data type?
    Positive and negative numbers can be stored in float variables, double, and long double data types on all platforms.
     
  2. Is it true that float is faster than double?
    When you don't need double-precision, you're memory-bandwidth constrained, and your hardware doesn't penalise floating; floats are faster than doubles. Because they take up half the space per number, they save memory bandwidth.
     
  3. How precise is double?
    A double, which we commonly implement with IEEE 754, is accurate to 15 to 17 decimal digits. Even if you can get the compiler to display it, you should not trust anything beyond that.
     
  4. In coding, what are doubles?
    The double data type is a built-in data type in the compiler used to define numeric variables containing decimal integers. The double is recognised as a type in C, C++, C#, and many other programming languages. A double type can represent both fractional and full numbers.

Key Takeaways

In this article, we discussed the Java Double class and its implementation. Different fields, methods and constructors associated with the Java Double class has been thoroughly explained with syntax and examples.

We hope that this blog has helped you enhance your knowledge regarding Java Double Class. If you would like to learn more, check out our articles on “Program to count the total number of characters in the string” and “selenium interview questions.” Do upvote our blog to help other ninjas grow.

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Happy Reading!

Live masterclass