Java Math.decrementExact() method returns the argument after decreasing it by one. In this tutorial, we will discuss decrementExact() method with examples.
public class JavaExample
{
  public static void main(String[] args)
  {
    int i = 50; //int
    long l = 16004L; //long
    System.out.println(Math.decrementExact(i));
    System.out.println(Math.decrementExact(l));
  }
}
Output:
49 16003
Syntax of decrementExact() method
Math.decrementExact(50); //returns 49
decrementExact() Description
public static int decrementExact(int a): Returns the int argument a after decreasing it by one. It throws ArithmeticException, if the result overflows an int (which means if the argument is Integer.MIN_VALUE, the decrement operation would throw the exception).
public static long decrementExact(long a): Returns the long argument a after decreasing it by one. It throws ArithmeticException, if the result overflows a long (which means if the argument is Long.MIN_VALUE, the decrement operation would throw the exception).
decrementExact() Parameters
- a: Int or long value that needs to be decremented.
decrementExact() Return Value
- Returns argument minus one (i.e. a-1).
Example 1: Decrement int value by one
public class JavaExample
{
  public static void main(String[] args)
  {
    int i = 160; //int
    System.out.print("Value of i, decremented by one: ");
    System.out.println(Math.decrementExact(i));
  }
}
Output:

Example 2: Decrement long value by one
public class JavaExample
{
  public static void main(String[] args)
  {
    long l = 160005L; //long
    System.out.print("Value of l, decremented by one: ");
    System.out.println(Math.decrementExact(l));
  }
}
Output:

Example 3: Integer overflow while calling decrementExact()
public class JavaExample
{
  public static void main(String[] args)
  {
    int i = Integer.MIN_VALUE;
    System.out.println(Math.decrementExact(i));
  }
}
Output:

Example 4: Long overflow while calling decrementExact()
public class JavaExample
{
  public static void main(String[] args)
  {
    long l = Long.MIN_VALUE;
    System.out.println(Math.decrementExact(l));
  }
}
Output:
