Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

How to cast  java.lang.Double to java.lang.Integer?

 

When I tried it, it throws an exception like below

 

"java.lang.ClassCastException: java.lang.Double incompatible with java.lang.Integer"

1 Answer

0 votes
by (19.7k points)

There’s no direct way to cast Integers to Double.  There’s a difference between double class and double primitive. Primitives start with lowercase characters (e.g. int, double ) and they don’t have methods. 

 

Whereas Double class acts as wrappers around the primitives, so it’s possible to have objects for it. In your case, you can use the method intValue() to get the value as a primitive int.  

 

If you want to learn more about Java? Check this out: Java course on Intellipaat     

Related questions

0 votes
0 answers
0 votes
1 answer
0 votes
1 answer

Browse Categories

...