开发者

Convert Object into primitive int

开发者 https://www.devze.com 2022-12-25 04:48 出处:网络
How to Convert an Object(not String),like TreeNode.item, into primitive开发者_运维问答 like int.In response to your last comment: just double-check, that the object is really of type Integer, then use

How to Convert an Object(not String),like TreeNode.item, into primitive开发者_运维问答 like int.


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();
}


hashCode() might be what you want. Then again, it might not.

0

精彩评论

暂无评论...
验证码 换一张
取 消