简体   繁体   中英

assign Integer type to int

Is there any problem (Run Time of course) in assigning Integer to int ?

int x= Integer.class.cast(parameters.get("PO_PAGE_CNT"));

As Integer is the parent class and int is the instance, I think maybe in some cases I get ClassCastException or other type of Exception at run time. Is it right?

Assuming that parameters.get("PO_PAGE_CNT") returns an instance of Integer , there is no problem with this assignment.

It would be more readable to simply write :

int x = (Integer) parameters.get("PO_PAGE_CNT");

An int can always be assigned to Integer (Boxing) and vice versa (Unboxing).

no problem. There is concept called boxing and unboxing. you can follow this link .

No, there is no problem. Integer values are automatically unboxed to int values and vice versa.

There is one exception, though. If parameters.get("PO_PAGE_CNT") returns null , you'll get a NullPointerException , since there is no int equivalent to null .

There is no problem. You are boxing and unboxing Integer. And there will be no ClassCastException if you are unboxing Integer to int

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