Integer i = theLong != null ? theLong.intValue() : null;
或如果您不必担心null:
// auto-unboxing does not go from Long to int directly, soInteger i = (int) (long) theLong;
在这两种情况下,您都可能会遇到溢出问题(因为Long可以比Integer存储更大的范围)。
Java 8有一个辅助方法来检查溢出(在这种情况下,您会得到一个异常):
Integer i = theLong == null ? null : Math.toIntExact(theLong);



