alan
alan

Reputation: 49

Convert Object into primitive int

How to Convert an Object(not String),like TreeNode.item, into primitive like int.

Upvotes: 0

Views: 15799

Answers (2)

Andreas Dolk
Andreas Dolk

Reputation: 114767

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

Upvotes: 6

Daniel Earwicker
Daniel Earwicker

Reputation: 116674

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

Upvotes: 0

Related Questions