日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 默认站点!

默认站点

當前位置: 首頁 >

Java中Long到Int的精确转换

發(fā)布時間:2023/12/3 27 豆豆
默认站点 收集整理的這篇文章主要介紹了 Java中Long到Int的精确转换 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

JDK 8附帶所有令人眼前一亮的 東西 ( lambda表達式 , 流 , Optional ,新的Date / Time API等)來分散我的注意力 ,我并沒有過多注意添加方法Math.toIntExact() 。 但是,這種小的添加本身可能會非常有用。

Math.toIntExact(long)的Javadoc文檔指出:“返回long參數的值; 如果值溢出int則引發(fā)異常。” 這在給定或已經具有Long且需要調用期望int的API的情況下特別有用。 當然,最好是將API更改為使用相同的數據類型,但這有時是無法控制的。 當需要將Long強制為int值時,則存在整數溢出的可能性,因為Long的數值可能比int可以準確表示的數值大。

如果有人告訴給定的Long永遠不會大于int可以容納的Math.toIntExact(Long) ,則靜態(tài)方法Math.toIntExact(Long)尤其有用,因為如果出現這種“異常”情況,它將拋出未經檢查的ArithmeticException ,這很明顯發(fā)生“例外”情況。

當Long.intValue()用于從Long獲取整數時,如果發(fā)生整數溢出,則不會引發(fā)異常。 而是提供了一個整數,但是由于整數溢出,該值很少有用。 在幾乎每種可能的情況下,遇到運行時異常都會向整數警報發(fā)出警報的情況要比讓軟件繼續(xù)錯誤地使用溢出數更好。

如在示出之間的差異的第一步Long.intValue()和Math.toIntExact(Long) ,下面的代碼生成的范圍內Long為5的值小于Integer.MAX_VALUE的至5以上Integer.MAX_VALUE 。

包含Integer.MAX_VALUE的Long的生成范圍

/*** Generate {@code Long}s from range of integers that start* before {@code Integer.MAX_VALUE} and end after that* maximum integer value.** @return {@code Long}s generated over range includes* {@code Integer.MAX_VALUE}.*/ public static List<Long> generateLongInts() {final Long maximumIntegerAsLong = Long.valueOf(Integer.MAX_VALUE);final Long startingLong = maximumIntegerAsLong - 5;final Long endingLong = maximumIntegerAsLong + 5;return LongStream.range(startingLong, endingLong).boxed().collect(Collectors.toList()); }

下一個代碼清單顯示了兩種方法,這些方法演示了前面提到的兩種從Long獲取int方法。

使用Long.intValue()和Math.toIntExact(Long)

/*** Provides the {@code int} representation of the provided* {@code Long} based on an invocation of the provided* {@code Long} object's {@code intValue()} method.** @param longRepresentation {@code Long} for which {@code int}* value extracted with {@code intValue()} will be returned.* @return {@code int} value corresponding to the provided* {@code Long} as provided by invoking the method* {@code intValue()} on that provided {@code Long}.* @throws NullPointerException Thrown if the provided long* representation is {@code null}.*/ public static void writeLongIntValue(final Long longRepresentation) {out.print(longRepresentation + " => Long.intValue() = ");try{out.println(longRepresentation.intValue());}catch (Exception exception){out.println("ERROR - " + exception);} }/*** Provides the {@code int} representation of the provided* {@code Long} based on an invocation of {@code Math.toIntExact(Long)}* on the provided {@code Long}.** @param longRepresentation {@code Long} for which {@code int}* value extracted with {@code Math.toIntExact(Long)} will be* returned.* @return {@code int} value corresponding to the provided* {@code Long} as provided by invoking the method* {@code Math.toIntExact)Long} on that provided {@code Long}.* @throws NullPointerException Thrown if the provided long* representation is {@code null}.* @throws ArithmeticException Thrown if the provided {@code Long}* cannot be represented as an integer without overflow.*/ public static void writeIntExact(final Long longRepresentation) {out.print(longRepresentation + " => Math.toIntExact(Long) = ");try{out.println(Math.toIntExact(longRepresentation));}catch (Exception exception){out.println("ERROR: " + exception);} }

當以上代碼使用在先前代碼清單中構建的Long的范圍( GitHub上提供完整代碼 )執(zhí)行時,輸出如下所示:

2147483642 => Long.intValue() = 2147483642 2147483642 => Math.toIntExact(Long) = 2147483642 2147483643 => Long.intValue() = 2147483643 2147483643 => Math.toIntExact(Long) = 2147483643 2147483644 => Long.intValue() = 2147483644 2147483644 => Math.toIntExact(Long) = 2147483644 2147483645 => Long.intValue() = 2147483645 2147483645 => Math.toIntExact(Long) = 2147483645 2147483646 => Long.intValue() = 2147483646 2147483646 => Math.toIntExact(Long) = 2147483646 2147483647 => Long.intValue() = 2147483647 2147483647 => Math.toIntExact(Long) = 2147483647 2147483648 => Long.intValue() = -2147483648 2147483648 => Math.toIntExact(Long) = ERROR: java.lang.ArithmeticException: integer overflow 2147483649 => Long.intValue() = -2147483647 2147483649 => Math.toIntExact(Long) = ERROR: java.lang.ArithmeticException: integer overflow 2147483650 => Long.intValue() = -2147483646 2147483650 => Math.toIntExact(Long) = ERROR: java.lang.ArithmeticException: integer overflow 2147483651 => Long.intValue() = -2147483645 2147483651 => Math.toIntExact(Long) = ERROR: java.lang.ArithmeticException: integer overflow

高亮顯示的行指示代碼處理Long的值等于Integer.MAX_VALUE的代碼。 之后,顯示的Long表示比Integer.MAX_VALUE多一個的Long并顯示嘗試使用Long.intValue()和Math.toIntExact(Long)將該Long轉換為int的結果。 Long.intValue()方法遇到整數溢出,但不會引發(fā)異常,而是返回負數-2147483648 。 Math.toIntExact(Long)方法在整數溢出時不返回任何值,而是引發(fā)ArithmeticException與信息性消息“整數溢出”。

Math.toIntExact(Long)方法的重要性不如JDK 8引入的許多功能重要,但是它對于避免有時可能難以診斷的整數溢出相關的錯誤類型很有用。

翻譯自: https://www.javacodegeeks.com/2018/06/exact-conversion-long-to-int-java.html

總結

以上是默认站点為你收集整理的Java中Long到Int的精确转换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得默认站点網站內容還不錯,歡迎將默认站点推薦給好友。