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 ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…