Integer i = theLong != null ? theLong.intValue() : null;
或如果您不必担心null:
// auto-unboxing does not go from Long to int directly, so
Integer i = (int) (long) theLong;
在这两种情况下,您都可能会遇到溢出(因为Long可以比Integer存储更大的范围)。
Java 8有一个辅助方法来检查溢出(在这种情况下,您会得到一个异常):
Integer i = theLong == null ? null : Math.toIntExact(theLong);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句