Java: many ways of casting a (long) Object to double
Solution 1
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.
Solution 2
I have an
Object obj
that I know is actually along
.
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();
jarek
Programming I program client and server side code in Go, Python, Java, JavaScript, CoffeeScript, SQL, Bash, Awk, Scala, C++ and many other languages using VSCode or Vim. Other skills: Linux, Git, Google Cloud, BigQuery, Kafka, PostgreSQL, Jenkins, Ansible, Hadoop (Cloudera). Science In 2017, I finished my dissertation on "Visualization-Driven Data Aggregation", digging deep into data management topics, such as dimensionality reduction, data aggregation, and relational algebra, but also into computer graphics topics, such as line rendering, line simplification, and data visualization in general. I publish scientific papers on top-level conferences, such as VLDB and IEEE BigData.
Updated on July 14, 2022Comments
-
jarek almost 2 years
I have an
Object obj
that I know is actually along
. In some Math code I need it asdouble
.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 aNumber
and not along
. - 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.
-
-
T.J. Crowder over 11 years"Long => double is not" Works for me, presumably the
Long
gets auto-unboxed. -
assylias over 11 years
Object o = 5L; double d = (double) o;
does not work. Butdouble d = (Long) o;
does. ;-) -
T.J. Crowder over 11 years@ assylias: I never said it did. :-) I said Long => double works. And it does, see my answer.
-
assylias over 11 yearsYou only need one cast:
double d = (Long) obj;
ordouble d = (long) obj;
-
T.J. Crowder over 11 years@assylias: I prefer the clarity, as the cast is there whether I write it or not. (And of course, in the above, I do need two casts. I could only rely on the implicit cast if I were assigning to a variable.) Also,
double d = (long)obj;
fails, you can't castObject
tolong
. -
assylias over 11 years
Object o = 15L; double d = (long) o;
compiles and runs without exception. -
T.J. Crowder over 11 years@assylias: On what? It doesn't for me on Oracle's JDK 1.6: pastie.org/5519551
-
T.J. Crowder over 11 years@Juve: Must be a Java 7 enhancement.
-
Neet over 11 yearsEhm, he said he has an 'Object' so there is no primitive
long
which can be casted to 'double'. -
jarek over 11 yearsI like this answer best. It shortly explains the boxing and I tried the
object instanceof Number
test via the debugger to see what is going on in my JVM. Thx!