Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: many ways of casting a (long) Object to double

I have an Object obj that I know is actually a long. In some Math code I need it as double.

Is it safe to directly cast it to double?

double x = (double)obj;

Or should I rather cast it first to long and then to double.

double x = (double)(long)obj;

I also found another (less readable) alternative:

double x = new Long((long)obj).doubleValue();

What are the dangers/implications of doing either?

Solution Summary:

  • obj is a Number and not a long.
  • Java 6 requires explicit casting, e.g.: double x = ((Number)obj).doubleValue()
  • Java 7 has working cast magic: double x = (long)obj

For more details on the Java6/7 issue also read discussion of TJ's answer.

Edit: I did some quick tests. Both ways of casting (explicit/magic) have the same performance.

like image 860
Juve Avatar asked Dec 13 '12 08:12

Juve


People also ask

Can double be cast to long in Java?

Approach 1 – Using Typecasting This is the simplest method to convert double to long data type in Java.

How do you cast something to a double?

There are three ways to convert a String to double value in Java, Double. parseDouble() method, Double. valueOf() method and by using new Double() constructor and then storing the resulting object into a primitive double field, autoboxing in Java will convert a Double object to the double primitive in no time.

How do you double an object in Java?

To convert double primitive type to a Double object, you need to use Double constructor. Let's say the following is our double primitive. // double primitive double val = 23.78; To convert it to a Double object, use Double constructor.


2 Answers

As every primitive number in Java gets cast to its boxing type when an object is needed (in our case Long) and every boxed number is an instance of Number the safest way for doing so is:

final Object object = 0xdeadbeefL;
final double d = ((Number)object).doubleValue();

The danger here is, as always, that the Object we want to cast is not of type Number in which case you will get a ClassCastException. You may check the type of the object like

if(object instanceof Number) ...

if you like to prevent class cast exceptions and instead supply a default value like 0.0. Also silently failing methods are not always a good idea.

like image 154
Neet Avatar answered Oct 24 '22 00:10

Neet


I have an Object obj that I know is actually a long.

No, you don't. long is a primitive data type, and primitive types in Java are not objects. Note that there's a difference between the primitive type long and java.lang.Long, which is a wrapper class.

You cannot cast a Long (object) to a long (primitive). To get the long value out of a Long, call longValue() on it:

Long obj = ...;

long value = obj.longValue();

Is it safe to directly cast it to double?

If it's actually a primitive long, then yes, you can cast that to a double. If it's a Long object, you don't need to cast, you can just call doubleValue() on it:

double x = obj.doubleValue();
like image 29
Jesper Avatar answered Oct 24 '22 02:10

Jesper