ClassCastException Integer to Double
Asked Answered
K

5

6

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?

Katelyn answered 30/3, 2015 at 14:2 Comment(2)
4 is automatically boxed to Integer, which cannot be casted to double.Blockage
Just as a hint, 4 can also be a double without casting.Entice
P
12

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.

Poindexter answered 30/3, 2015 at 14:7 Comment(0)
C
5

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);
Clientele answered 30/3, 2015 at 14:9 Comment(0)
T
1

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;
Tall answered 30/3, 2015 at 14:7 Comment(0)
I
1

The first attempt converts 4 into an object of type Integer which basically is a container that holds the int value of 4.

The second attempt just casts a int into a double with both being primitive non-object types.

You can't cast an object into a primitive type.

Infer answered 30/3, 2015 at 14:7 Comment(2)
You can cast an Object to a primitive type, as long as it's the right type. Object o = 1; int a = (int) o; works.Buckling
It would be best not to cast Objects as primitives. Defeats the purpose of using Objects.Infer
O
1

Your problem is about boxing and unboxing. You can read more about that here : https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

Odaniel answered 30/3, 2015 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.