简体   繁体   中英

Type Casting in Java for Boxed Class?

With Possible Un-boxing in Java Why can't I Downcast With Integer Class.

double a=20.3;
int b=(Integer)a;

It gives me error incompatible Type Conversion. My question is why Integer class is not able to Downcast it like

int b=(int)a;

Does Casting works with class for primitive type in Java?

double can't autobox into Integer. However, int can autobox into Integer. See docs here.

    double a=20.3;
    Integer b = (int) a;
    int c = b; // c = 20;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM