Java convert String to double example

What is the difference between Double and double in Java?

Double is a class while double is a primitive. In other words, Double has a bunch of methods while all you can do with double is use it in arithmetical expressions.

Constructor Summary
  • Double(double value)
    • Constructs a newly allocated Double object that represents the primitive double argument.
  • Double(String s)
    • Constructs a newly allocated Double object that represents the floating-point value of type double represented by the string.
Method Summary
  • static Double valueOf(double d)
    • Returns a Double instance representing the specified double value.
  • static Double valueOf(String s)
    • Returns a Double object holding the double value represented by the argument string s.

Click here for java.lang.Double API docs
package com.as400samplecode;

public class ConvertStringToDouble {

    public static void main(String[] args) {

        String myString = "400.99";
        double mydouble = Double.parseDouble(myString);
        System.out.println(mydouble);

        Double myDouble = new Double(myString);
        System.out.println(myDouble);

        myDouble = Double.valueOf(myString);
        System.out.println(myDouble);

    }

}

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.