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 数学方法

