English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Java Math Mathematical Methods
The Java Math decrementExact() method subtracts from the specified variable1and returns it.
The syntax of the decrementExact() method is:
Math.decrementExact(num)
Note: decrementExact() is a static method. Therefore, we can access this method using the Math class name.
num - Subtracting1fromParameter
Note: The data type of the parameter should be int or long.
Subtracting from the parameter1Returns the value after
class Main { public static void main(String[] args) { //Create an int variable int a = 65; // With int argument decrementExact()t System.out.println(Math.decrementExact(a)); // 64 // Create a long variable long c = 52336L; // Using LONG parameter decrementExact() System.out.println(Math.decrementExact(c)); // 52335 } }
In the above example, we used the Math.decrementExact() method with int and long variables to subtract from their respective variables1.
If the result of the subtraction exceeds the data type, the decrementExact() method will throw an exception. That is, the result should be within the range of the specified variable's data type.
class Main { public static void main(String[] args) { //Create an int variable //Minimum int value int a = -2147483648; //Using decrementExact() with int parameter //Trigger an exception System.out.println(Math.decrementExact(a)); } }
In the above example, the value of a is the minimum value of int. Here, the decrementExact() method subtracts from it1a.
a - 1 => -2147483648 - 1 => -2147483649 // out of range of int type
Therefore, the decrementExact() method triggers an integer overflow exception.