Ok 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 starting (Eclipse gives no errors)
java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer
Although 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);
Now nothing is wrong.
Why should you first transfer it to double?
source
share