Java Math toIntExact() 方法
示例
将 long 值转换为 int
System.out.println(Math.toIntExact(1500000L));
System.out.println(Math.toIntExact(-32L));
System.out.println(Math.toIntExact(-86345L));
System.out.println(Math.toIntExact(25L));
定义和用法
toIntExact() 方法将 long 值转换为 int,如果转换导致溢出则抛出异常。这可以防止因溢出而产生不正确的结果。
语法
public static int toIntExact(long x)参数值
| 参数 | 描述 | 
|---|---|
| x | 必需。要取反的整数。 | 
技术详情
| 返回 | 一个 int值,表示long整数的内容。 | 
|---|---|
| 抛出 | ArithmeticException- 如果转换导致溢出。 | 
| Java 版本 | 1.8+ | 
❮ Math 方法
 
