Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassCastException Integer to Double

Why this code throws this exception:

public class DS3{
    public static void main(String[] args) {
        double r = (double)((Object)4);
        System.out.println(r);          
    }   
}

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double

And this, just run fine:

public class DS4{
        public static void main(String[] args) {
            double r = (double)(4);
            System.out.println(r);          
        }   
    }

Both are a attempt to convert integer to double, right?

like image 627
Savrige Avatar asked Mar 30 '15 14:03

Savrige


People also ask

How do you convert an int to a double in C++?

if i'm not wrong you can directly assign an integer to a double in c++, there's no need to do an explicit casting. It's called "typecasting". int a = 5; double b = (double) a; double c = 4.0; int d = (int) c; chandra.

How do you solve ClassCastException?

// type cast an parent type to its child type. In order to deal with ClassCastException be careful that when you're trying to typecast an object of a class into another class ensure that the new type belongs to one of its parent classes or do not try to typecast a parent object to its child type.

What does ClassCastException mean?

ClassCastException is a runtime exception raised in Java when we try to improperly cast a class from one type to another. It's thrown to indicate that the code has attempted to cast an object to a related class, but of which it is not an instance.


3 Answers

The two conversions that you show, namely,

Object x = 4;
double r = (double)x;

and

double r = (double)(4);

require a different number of conversions:

  • The second conversion needs a single cast from an int to a double,
  • The first conversion needs an unboxing conversion from Object followed by a cast.

Java cast operator performs only one conversion at a time.

To make the first conversion work you have to add another cast to Integer, like this (demo):

double r = (double)((Integer)((Object)4));
System.out.println(r);
like image 32
Sergey Kalinichenko Avatar answered Oct 22 '22 19:10

Sergey Kalinichenko


Both are a attempt to convert integer to double, right?

Yes, and no.

This line

double r = (double)((Object)4);

causes the compiler to box the 4 in an Integer, and an Integer can't be cast to a double.

The bytecode for this snippet:

(double)((Object) 4)

Looks as follows:

// ...
5: iconst_4
6: invokestatic  #2    // Method Integer.valueOf
9: checkcast     #3    // class java/lang/Double
// ...

(Line 6 causes the boxing, line 9 throws the exception.)

In other words, it's equivalent to

Object tmp = (Object) 4;  // Auto-boxing to Integer
double d = (double) tmp;  // Illegal cast from Integer to double.

Here on the other hand

double r = (double)(4);

4 is regarded as an ordinary int, which can be cast to a double.

like image 78
aioobe Avatar answered Oct 22 '22 18:10

aioobe


In your first example, 4 is autoboxed to an Integer, which cannot then be cast to a primitive double.

Perhaps what you want is simply:

double r = 4;
like image 1
Duncan Jones Avatar answered Oct 22 '22 20:10

Duncan Jones