How to cast an Object to an int

后端 未结 19 2076
长情又很酷
长情又很酷 2020-11-27 09:58

How can I cast an Object to an int in java?

19条回答
  •  半阙折子戏
    2020-11-27 10:19

    Assuming the object is an Integer object, then you can do this:

    int i = ((Integer) obj).intValue();
    

    If the object isn't an Integer object, then you have to detect the type and convert it based on its type.

提交回复
热议问题