Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java.lang.Boolean Class in Java
Next article icon

Java.Lang.Double Class in Java

Last Updated : 03 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
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


Next Article
Java.lang.Boolean Class in Java

R

Rishabh Mahrsee
Improve
Article Tags :
  • Java
  • Java-Classes
  • Java-lang package
  • java-wrapper-class
Practice Tags :
  • Java

Similar Reads

  • Wrapper Classes in Java
    A Wrapper class in Java is one whose object wraps or contains primitive data types. When we create an object in a wrapper class, it contains a field, and in this field, we can store primitive data types. In other words, we can wrap a primitive value into a wrapper class object. Let's check on the wr
    6 min read
  • Primitive Wrapper Classes are Immutable in Java
    In Java, an immutable class is a class (Integer, Byte, Long, Float, Double, Character, Boolean, and Short) which once created then its body can not be changed and the same applies to immutable objects which once created cannot be changed. Now the question arises that we do need the help of wrapper c
    3 min read
  • Java.lang.Number Class in Java
    Most of the time, while working with numbers in java, we use primitive data types. But, Java also provides various numeric wrapper sub classes under the abstract class Number present in java.lang package. There are mainly six sub-classes under Number class.These sub-classes define some useful method
    9 min read
  • Java.lang.Integer class in Java
    Integer class is a wrapper class for the primitive type int which contains several methods to effectively deal with an int value like converting it to a string representation, and vice-versa. An object of the Integer class can hold a single int value. Constructors: Integer(int b): Creates an Integer
    15 min read
  • Java.Lang.Byte class in Java
    In Java, Byte class is a wrapper class for the primitive type byte which contains several methods to effectively deal with a byte value like converting it to a string representation, and vice-versa. An object of the Byte class can hold a single byte value. Constructors of Byte Class There are mainly
    6 min read
  • Java.Lang.Short class in Java
    Short class is a wrapper class for the primitive type short which contains several methods to effectively deal with a short value like converting it to a string representation, and vice-versa. An object of Short class can hold a single short value. There are mainly two constructors to initialize a S
    6 min read
  • Java.Lang.Long class in Java
    Long class is a wrapper class for the primitive type long which contains several methods to effectively deal with a long value like converting it to a string representation, and vice-versa. An object of Long class can hold a single long value. There are mainly two constructors to initialize a Long o
    12 min read
  • Java.Lang.Float class in Java
    Float class is a wrapper class for the primitive type float which contains several methods to effectively deal with a float value like converting it to a string representation, and vice-versa. An object of the Float class can hold a single float value. There are mainly two constructors to initialize
    6 min read
  • Java.Lang.Double Class in Java
    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 pr
    4 min read
  • Java.lang.Boolean Class in Java
    Java provides a wrapper class Boolean in java.lang package. The Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field, whose type is boolean. In addition, this class provides useful methods like to convert a boolean to a String and
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences