簡體   English   中英

將對象轉換為原始int

[英]Convert Object into primitive int

如何將Object(不是String)(例如TreeNode.item)轉換為基本類型(例如int)。

回應您的最后一條評論:只需仔細檢查一下,該對象確實是Integer類型,然后使用自動裝箱(我假設您的編譯器級別為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()可能就是您想要的。 再說一次,可能不會。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM