Any way to cast java.lang.Double
to java.lang.Integer
?
It throws an exception
"java.lang.ClassCastException: java.lang.Double incompatible with java.lang.Integer"
Any way to cast java.lang.Double
to java.lang.Integer
?
It throws an exception
"java.lang.ClassCastException: java.lang.Double incompatible with java.lang.Integer"
I think it's impossible to understand the other answers without covering the pitfalls and reasoning behind it.
You cannot directly cast an
Integer
to aDouble
object. AlsoDouble
andInteger
are immutable objects, so you cannot modify them in any way.Each numeric class has a primitive alternative (
Double
vsdouble
,Integer
vsint
, ...). Note that these primitives start with a lowercase character (e.g.int
). That tells us that they aren't classes/objects. Which also means that they don't have methods. By contrast, the classes (e.g.Integer
) act like boxes/wrappers around these primitives, which makes it possible to use them like objects.Strategy:
To convert a
Double
to anInteger
you would need to follow this strategy:Double
object to a primitivedouble
. (= "unboxing")double
to a primitiveint
. (= "casting")int
back to anInteger
object. (= "boxing")In code:
Actually there is a shortcut. You can unbox immediately from a
Double
straight to a primitiveint
. That way, you can skip step 2 entirely.Pitfalls:
However, there are a lot of things that are not covered in the code above. The code-above is not null-safe.
Now it works fine for most values. However integers have a very small range (min/max value) compared to a
Double
. On top of that, doubles can also hold "special values", that integers cannot:So, depending on the application, you may want to add some filtering to avoid nasty Exceptions.
Then, the next shortcoming is the rounding strategy. By default Java will always round down. Rounding down makes perfect sense in all programming languages. Basically Java is just throwing away some of the bytes. In financial applications you will surely want to use half-up rounding (e.g.:
round(0.5) = 1
andround(0.4) = 0
).Auto-(un)boxing
You could be tempted to use auto-(un)boxing in this, but I wouldn't. If you're already stuck now, then the next examples will not be that obvious neither. If you don't understand the inner workings of auto-(un)boxing then please don't use it.
I guess the following shouldn't be a surprise. But if it is, then you may want to read some article about casting in Java.
Prefer valueOf:
Also, don't be tempted to use
new Integer()
constructor (as some other answers propose). ThevalueOf()
methods are better because they use caching. It's a good habit to use these methods, because from time to time they will save you some memory.You need to explicitly get the int value using method intValue() like this:
Or
One should also add that it works with autoboxing.
Otherwise, you get an int (primitive) and then can get an Integer from there:
Call
intValue()
on yourDouble
object.Indeed, the simplest way is to use
intValue()
. However, this merely returns the integer part; it does not do any rounding. If you want the Integer nearest to the Double value, you'll need to do this:And don't forget the null case:
Math.round()
handles odd duck cases, like infinity and NaN, with relative grace.You can do that by using "Narrowing or Explicit type conversion", double → long → int. I hope it will work.
PS: It will give 0 as double has all the decimal values and nothing on the left side. In case of 0.58, it will narrow it down to 0. But for others it will do the magic.