Java correct way convert/cast object to Double

149,834

Solution 1

You can't cast an object to a Double if the object is not a Double.

Check out the API.

particularly note

valueOf(double d);

and

valueOf(String s);

Those methods give you a way of getting a Double instance from a String or double primitive. (Also not the constructors; read the documentation to see how they work) The object you are trying to convert naturally has to give you something that can be transformed into a double.

Finally, keep in mind that Double instances are immutable -- once created you can't change them.

Solution 2

new Double(object.toString());

But it seems weird to me that you're going from an Object to a Double. You should have a better idea what class of object you're starting with before attempting a conversion. You might have a bit of a code quality problem there.

Note that this is a conversion, not casting.

Solution 3

If your Object represents a number, eg, such as an Integer, you can cast it to a Number then call the doubleValue() method.

Double asDouble(Object o) {
    Double val = null;
    if (o instanceof Number) {
        val = ((Number) o).doubleValue();
    }
    return val;
}

Solution 4

You can use the instanceof operator to test to see if it is a double prior to casting. You can then safely cast it to a double. In addition you can test it against other known types (e.g. Integer) and then coerce them into a double manually if desired.

    Double d = null;
    if (obj instanceof Double) {
        d = (Double) obj;
    }

Solution 5

In Java version prior to 1.7 you cannot cast object to primitive type

double d = (double) obj;

You can cast an Object to a Double just fine

Double d = (Double) obj;

Beware, it can throw a ClassCastException if your object isn't a Double

Share:
149,834

Related videos on Youtube

user710818
Author by

user710818

Updated on July 09, 2022

Comments

  • user710818
    user710818 almost 2 years

    I need to convert Object o to Double. Is the correct way to convert it to String first?

    • nos
      nos over 12 years
      What is the special type of your Object ? Is it actually a Double, or something else ?
    • hasan.alkhatib
      hasan.alkhatib over 12 years
      What is Object? Is it an unknown class or a Double class that has been typed to an Object.
    • Saket
      Saket over 12 years
      "I try to found, but all seems vague." - your question is too vague to get an obvious answer. please elaborate. What is 'Object o' and what's the intention?
  • Alexey Romanov
    Alexey Romanov over 10 years
    Do you mean "prior to 1.5"? That's where generics and autoboxing were introduced. Or did you mean something else?
  • Ian Campbell
    Ian Campbell over 10 years
    Oops you are correct @AlexeyRomanov, I have edited my answer to reflect this.
  • Loolooii
    Loolooii over 7 years
    Double.valueOf(object.toString()) uses less memory
  • Ripley
    Ripley over 5 years
    Works great when type of the object is a value type.
  • Raj Saraogi
    Raj Saraogi over 4 years
    If the object can be double or integer so in that case what should we prefer ?
  • Jarek Przygódzki
    Jarek Przygódzki over 4 years
    @Raj Saraogi : Both Double and Integer implement java.lang.Number.There's no equivalent for primitive types.