Convert Long into Integer

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

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

14条回答
  •  感动是毒
    2020-12-07 08:15

    In addition to @Thilo's accepted answer, Math.toIntExact works also great in Optional method chaining, despite it accepts only an int as an argument

    Long coolLong = null;
    Integer coolInt = Optional.ofNullable(coolLong).map(Math::toIntExact).orElse(0); //yields 0
    

提交回复
热议问题