Skip to content
Advertisement

Cast Double to Integer in Java

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”

Advertisement

Answer

A Double is not an Integer, so the cast won’t work. Note the difference between the Double class and the double primitive. Also note that a Double is a Number, so it has the method intValue, which you can use to get the value as a primitive int.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement