On this document we will be showing a java example on how to use the decrementExact() method of Math Class. The decrementExact() Returns the argument decremented by one, throwing an exception if the result overflows the specified datatype either long or int depending on which data type has been used on the method argument. Make a note that the decrementExact() method is overloaded. Below are the two overloaded method of the decrementExact() method:

  • public static int decrementExact(int a)
  • public static long decrementExact(long a)

The two overloaded methods are basically the same, it’s just that they deal with different data type either int or long.

Most of the methods of the Math class is static and the decrementExact() method is no exception. Thus don’t forget that in order to call this method, you don’t have to create a new object. Use the method in the format Math.decrementExact(a).

From the first part we have described this method to throw an exception if the result overflows the specified data type. Since this is decrement, the only scenario that we will be hitting an exception if the result is less than the minimum value. The minimum value can be derived from Long.MIN_VALUE or Integer.MIN_VALUE.

Method Syntax

public static int decrementExact(int a)

public static long decrementExact(long a)

Method Returns

The decrementExact() method returns the results.


Requires Java 1.8 and up

Java Math decrementExact() Example

Below is a java code demonstrates the use of decrementExact() method of Math class. The example presented might be simple however it shows the behavior of the decrementExact() method.

The above java example source code demonstrates the use of decrementExact() method of Math class. We simply ask for a user input and we use the Scanner class to parse it. Since we have used the nextLine() method to get the console value which is having a return data type of String thus we have used the Integer.parseInt  to transform it into int. Alternatively if the requirements is to use long then you must use the Long.ParseLong() instead. This conversion is required because the argument for decrementExact() method only accepts either int or long.

Sample Output

Below is the sample output when you run the above example.

java lang Math decrementExact() example output