Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm Facing Issues at the Run time to compile this piece of code Which is throwing me an error that java.lang.Integer cannot be cast to Java.lang.Double. I'll more than happy if anyone help me out to correct this code

 double x;

 public Double getMethod() {

     HashMap hashmap= new HashMap();

     hashmap = SumCal();

     List  listabc = (List) hashmap.get("abclist");
     int total=(Integer) hashmap.get("all_total");
     x = (Double) listabc.get(0)*100/total;
     return x;
    }
share|improve this question
1  
It's because you can't cast an Integer to a Double. They're two different classes and one's not a subclass of the other. Most likely the element you're mysteriously fetching from listabc is an Integer. (You're confusing the hell out of yourself because you're depending on implicit "boxing" conversions, not wise when you're just learning.) –  Hot Licks Feb 23 '13 at 2:39
    
Please look at class hierarchy in Java before asking questions like this one. –  ATrubka Feb 23 '13 at 4:58
    
In fairness, the C-based languages confuse the issue enormously by using "cast" (both the term and the notation) for two essentially unrelated concepts -- data format conversion and reference hierarchy manipulation. Casting "float" to "int" is an entirely different and unrelated concept from casting "Number" to "Integer", eg. (And I can't recall ever seeing this confusing dichotomy discussed in textbooks/references, or hearing of it being discussed in the classroom.) –  Hot Licks Feb 25 '13 at 2:36

3 Answers 3

up vote 2 down vote accepted

You can do as below, But I would advice you to go with Generics.

x = ((Integer) listabc.get(0) * 100 / total);

If you had used the Generics like below you dont need any casting.

List<Integer> listabc 
HashMap<String, Integer> hashmap 
x = listabc.get(0) * 100 / total;

In that case you dont need any casting. One of the reason Integer, Double etc wrapper classes were introduced is to avoid casting.

share|improve this answer

First, you should not use a generic HashMap, because then the compiler can't help you with types. Try declaring the following:

HashMap<String, Integer> hashmap = new HashMap();

I would not recommend storing a HashMap of differently-valued types, as it just invites programming errors. Also, there's no need to cast an int to a Double. You can do any of the following:

x = listabc.get(0) * 100.0 / total; // multiply by a floating-point number

x = (double) listabc.get(0) * 100 / total; // cast with (double)
share|improve this answer

A slight change in your code will grant success:

x = (double) listabc.get(0)*100/total;

Integer and Double are not cast compatible, but int and double are widening compatible. In your current code, the right hand side (RHS) of your expression is getting autoboxed up to an Integer, then you specify a cast of that result to Double. This cast fails.

By instead specifying a widening conversion, the Integer result gets unboxed into an int, then converted to a double. Then finally this line:

return x;

Boxes up the value of x into a Double and returns it as the result of the method.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.