English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java array

Java object-oriented (I)

Java object-oriented (II)

Java object-oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map collection

Java Set collection

Java input and output (I/O)

Java Reader/Writer

Java other topics

Java Math decrementExact() usage and example

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.

decrementExact() parameter

  • num - Subtracting1fromParameter

Note: The data type of the parameter should be int or long.

Returns the value of decrementExact()

  • Subtracting from the parameter1Returns the value after

Example1: Java Math.decrementExact()

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.

Example2: Math.decrementExact() throws an exception

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.

Recommended Tutorials

Java Math Mathematical Methods