将Object转换为原始int [英] Convert Object into primitive int
本文介绍了将Object转换为原始int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将对象(非字符串)(如TreeNode.item)转换为类似int的原语。
How to Convert an Object(not String),like TreeNode.item, into primitive like int.
推荐答案
作为回应你的最后评论:只需仔细检查,该对象实际上是Integer类型,然后使用自动装箱(我假设您的编译器级别为1.5 +):
In response to your last comment: just double-check, that the object is really of type Integer, then use auto-boxing (I assume that your compiler level is 1.5+):
Object o = getTheValue();
int result = 0; // we have to initialize it here!
if (o instanceof Integer) {
result = (Integer) o;
} else {
throw new WTFThisShouldHaveBeenIntegerException();
}
这篇关于将Object转换为原始int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文