Okay, so if I have this code
double a=1.5;
int b=(int)a;
System.out.println(b);
Everything works fine, but
Object a=1.5;
int b=(int)a;
System.out.println(b);
gives the following error after running (Eclipse doesn't give any error)
java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer
Though, when I do
Object a=1.5;
double b=(double)a;
int c=(int)b;
System.out.println(c);
or
Object a=1.5;
int b=(int)(double)a;
System.out.println(b);
Nothing's wrong again.
Why do you have to cast it to double
first ?
I am not sure why your code works at all. You should not be able to cast Object to 'double' because they are incompatible types. Also casting type int to double are incompatible types. Your first block of code:
will print "1". You will lose the decimals. If you want to just print the number before the decimal point then you can format your double when you print and you won't need to cast to type int.
But the reason the other don't work is because you are trying to cast to an incompatible type. It is strange that you say the last two blocks of code
These should not work because of incompatible types.
When you declare the object
Object a = 1.5
you can tell by checkingSystem.out.println(a.getClass())
that the object is in fact cast to aDouble
instance. This can again be cast to adouble
because of unboxing conventions. After that thedouble
value can be cast to anint
.There are however no unboxing conventions to cast from a Double instance to an
int
, so the runtime will issue anClassCastException
if you try and do that. It cannot directly go fromDouble
toInteger
.When you're casting from
Object
, you're unboxing from the wrapper type... and you can only unbox to the original type, basically. It's effectively a cast to the relevant wrapper type, followed by a call to the appropriatexxxValue
method. So this:is equivalent to:
That cast to
Double
will obviously fail ifx
isn't a reference to aDouble
.So your problematic code is equivalent to:
Hopefully now it's obvious why that would fail - because the first line creates a
Double
which you're then trying to cast toInteger
.