1

Say I have this:

Object obj = new Double(3.14);

Is there a way to use obj like a Double without explicitly casting it to Double? For instance, if I wanted to use the .doubleValue() method of Double for calculations.

4
  • 3
    Why don't you want to cast? Commented Jun 12, 2013 at 14:50
  • Because I want a truly generic means of handling Objects that are in a List and explicit casting would require the I write a ton of if-clauses. My curiosity lies in creating a more dynamic means of doing with Objects what I will/need. Commented Jun 12, 2013 at 15:01
  • Are generics not "truly generic" enough for you? Commented Jun 12, 2013 at 15:07
  • I apologize for the context switch, I though my original question would lead me to an answer but only left be feeling like I needed to explain more. Commented Jun 12, 2013 at 15:45

6 Answers 6

5

No, there is no way to do this. obj is of type Object (even though it is a Double instance), and the Object class does not have such methods as doubleValue(). The proper way would indeed be to cast:

Double d = (Double) obj;
Sign up to request clarification or add additional context in comments.

Comments

3

The only way to do it if you can not cast is to use reflection:

Object obj = new Double(3.14);

Method m1 = obj.getClass().getMethod("doubleValue");
System.out.println("Double value: " + m1.invoke(obj));

Method m2 = obj.getClass().getMethod("intValue");
System.out.println("Int value: " + m2.invoke(obj));
Double value: 3.14
Int value: 3

This is usually only useful in some limited corner cases - normally, casting, generics, or using some supertype is the right approach.

1 Comment

This is extremely useful! Thanks! It does not completely solve my issue but it certainly will help in the corner cases I do have to deal with.
2

You cannot.

Since your reference is to an Object, you will only have the methods which Object has at its disposal.

Note that you can use Number, which is the superclass of Integer, Short, etc and which defines .doubleValue():

final Number n = new Double(2.0);
n.doubleValue(); // works

Comments

2

The closest you can do is this

Number num = new Double(3.14);
double d= num.doubleValue();

You can only call methods that the compiler knows is available, not based on the runtime type of the objects.

In short Object doesn't have a doubleValue() method so you cannot call it. You have to have a reference type which has the method you want to call.

2 Comments

So regardless, at runtime if I've got an Object there is absolutely no way to somehow use it like a Number/Double/etc.?
@Mastergeek not without casting it first. You can toString(), hashCode(), equals() as Object has those methods.
1

No, it's not possible. The instance obj is a reference to an Object and can see only the methods of the Object class. You must cast it to Double to use specific methods of the Double class.

Comments

0

No! The only alternative is to use:

obj.toString()

Or, to use as double:

Double.parseDouble(obj.toString())

But it is not a good practice. Certainly has some another good alternative to your case. Post your code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.