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:
OptionalDouble of(double) method in Java with examples
Next article icon

Double parseDouble() method in Java with examples

Last Updated : 26 Oct, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report

The parseDouble() method of Java Double class is a built in method in Java that returns a new double initialized to the value represented by the specified String, as done by the valueOf method of class Double.

Syntax:

public static double parseDouble(String s)

Parameters: It accepts a single mandatory parameter s which specifies the string to be parsed.

Return type: It returns e double value represented by the string argument.

Exception: The function throws two exceptions which are described below:

  • NullPointerException– when the string parsed is null
  • NumberFormatException– when the string parsed does not contain a parsable float

Below is the implementation of the above method.

Program 1:




// Java Code to implement
// parseDouble() method of Double class
  
class GFG {
  
    // Driver method
    public static void main(String[] args)
    {
  
        String str = "100";
  
        // returns the double value
        // represented by the string argument
        double val = Double.parseDouble(str);
  
        // prints the double value
        System.out.println("Value = " + val);
    }
}
 
 
Output:
  Value = 100.0  

Program 2: To show NumberFormatException




// Java Code to implement
// parseDouble() method of Double class
  
class GFG {
  
    // Driver method
    public static void main(String[] args)
    {
  
        try {
  
            String str = "";
  
            // returns the double value
            // represented by the string argument
            double val = Double.parseDouble(str);
  
            // prints the double value
            System.out.println("Value = " + val);
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}
 
 
Output:
  Exception: java.lang.NumberFormatException: empty String  

Program 3: To show NullPointerException




// Java Code to implement
// parseDouble() method of Double class
  
class GFG {
  
    // Driver method
    public static void main(String[] args)
    {
  
        try {
  
            String str = null;
  
            // returns the double value
            // represented by the string argument
            double val = Double.parseDouble(str);
  
            // prints the double value
            System.out.println("Value = " + val);
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}
 
 
Output:
  Exception: java.lang.NullPointerException  

Reference: https://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#parseDouble(java.lang.String)



Next Article
OptionalDouble of(double) method in Java with examples
author
gopaldave
Improve
Article Tags :
  • Java
  • java-basics
  • Java-Double
  • Java-Functions
  • Java-lang package
Practice Tags :
  • Java

Similar Reads

  • OptionalDouble of(double) method in Java with examples
    The of(double) method help us to get an OptionalDouble object which contains a double value which is passed as a parameter to this method. Syntax: public static OptionalDouble of(double value) Parameters: This method accepts a double value as parameter that will be set to the returned OptionalDouble
    1 min read
  • OptionalDouble orElse(double) method in Java with examples
    The orElse(double) method helps us to get the value in this OptionalDouble object. If a value is not present in this OptionalDouble, then this method returns the value passed as the parameter. Syntax: public double orElse(double other) Parameters: This method accepts double the value to be returned,
    1 min read
  • DoubleAdder sum() method in Java with Examples
    The java.DoubleAdder.sum() is an inbuilt method in java that returns the current sum. When the object of the class is created its initial value is zero. Syntax: public double sum() Parameters: This method does not accepts any parameter. Return Value: This method returns the current sum. Below progra
    1 min read
  • DoubleAdder reset() method in Java with Examples
    The java.DoubleAdder.reset() is an inbuilt method in java that resets variables maintaining the sum to zero. When the object of the class is created its initial value is zero. Syntax: public void reset() Parameters: This method does not accepts any parameter. Return Value: The method returns the res
    1 min read
  • Number.doubleValue() method in java with examples
    The java.lang.Number.doubleValue() is an inbuilt method in java that returns the value of the specified number casted as a double data type. This may involve rounding or truncation. Syntax: public abstract double doubleValue() Parameters: This method does not accepts any parameter. Return value: Thi
    1 min read
  • Short doubleValue() method in Java with Examples
    The java.lang.Short.doubleValue() method of Short class is a built in method in Java which is used to return the value of the Short object as a double. Syntax: public double doubleValue() Return type: It return the value of ShortObject as double. Below is the implementation of doubleValue() method i
    2 min read
  • OptionalDouble equals() method in Java with examples
    OptionalDouble help us to create an object which may or may not contain a Double value. The equals(Object obj) method help us to compare this OptionalDouble object with the passed object as a parameter and it returns true if objects are equal. The other object is considered equal to this OptionalDou
    2 min read
  • Field setDouble() method in Java with Examples
    setDouble() method of java.lang.reflect.Field used to set the value of a field as a double on the specified object. When you need to set the value of a field of an object as double then you can use this method to set value over an Object. Syntax: public void setDouble(Object obj, double d) throws Il
    3 min read
  • DataInputStream readDouble() method in Java with Examples
    The readDouble() method of DataInputStream class in Java is used to read eight input bytes and returns a double value. This method reads the next eight bytes from the input stream and interprets it into double type and returns. Syntax: public final double readDouble() throws IOException Specified By
    2 min read
  • PrintStream print(double) method in Java with Examples
    The print(double) method of PrintStream Class in Java is used to print the specified double value on the stream. This double value is taken as a parameter. Syntax: public void print(double doubleValue) Parameters: This method accepts a mandatory parameter doubleValue which is the double value to be
    2 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