Java Math decrementExact() 方法

定义和用法

decrementExact() 方法返回比指定整数小 1 的值,如果发生溢出,则抛出一个异常。这可以防止因溢出而产生的不正确结果。

实例

递减不同的数字:

System.out.println(Math.decrementExact(12002));
System.out.println(Math.decrementExact(18));
System.out.println(Math.decrementExact(32));
System.out.println(Math.decrementExact(947));

亲自试一试

语法

以下之一:

public static int decrementExact(int x)
public static long decrementExact(long x)

参数

参数 描述
x 必需。要递减的整数。

技术细节

返回: 表示比整数小 1 的 intlong 值。
抛出: ArithmeticException - 如果递减导致溢出。
Java 版本: 1.8+