5

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;
    }
3
  • 2
    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.) Commented Feb 23, 2013 at 2:39
  • Please look at class hierarchy in Java before asking questions like this one. Commented Feb 23, 2013 at 4:58
  • 2
    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.) Commented Feb 25, 2013 at 2:36

3 Answers 3

6

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.

0
5

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.

0

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.