Convert Long into Integer

后端 未结 14 2147
伪装坚强ぢ
伪装坚强ぢ 2020-12-07 07:48

How to convert a Long value into an Integer value in Java?

14条回答
  •  情书的邮戳
    2020-12-07 08:06

    In Java 8 you can use Math.toIntExact. If you want to handle null values, use:

    Integer intVal = longVal == null ? null : Math.toIntExact(longVal);
    

    Good thing about this method is that it throws an ArithmeticException if the argument (long) overflows an int.

提交回复
热议问题