首页 > 编程知识 正文

java int类型转换为string类型,object类型转换成int

时间:2023-05-06 04:44:33 阅读:29947 作者:3245

howcanicastanobjecttoanintinjava?

if you ' resurethatthisobjectisaninteger :

intI=(integer ) object;

Or,starting from Java 7,you can equivalently write:

intI=(int ) object;

Beware,itcanthrowaclasscastexceptionifyourobjectisn ' tanintegerandanullpointerexceptionifyourobjectisnull。

thiswayyouassumethatyourobjectisaninteger (thewrappedint ) and you unbox it into an int。

intisaprimitivesoitcan ' tbestoredasanobject,theonlywayistohaveanintconsidered/boxedasanintegerthenstoredasanobject。

If your object is a String,thenyoucanusetheinteger.value of (methodtoconvertitintoasimpleint 3360

inti=integer.value of ((字符串) object;

itcanthrowanumberformatexceptionifyourobjectisn ' treallyastringwithanintegerascontent。

资源:

On the same topic :

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。