Java .decrementExact()

Anonymous contributor's avatar
Anonymous contributor
Published Oct 23, 2022
Contribute to Docs

The Math.decrementExact() method returns the argument decremented by one. It throws an exception if the result overflows the specified data type of the argument.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours

Syntax

Math.decrementExact(num)

The num parameter is of type long or int.

Example

The following example uses the .decrementExact() method to decrement the input by 1:

// Main.java
public class Main {
public static void main(String[] args) {
int value = 21;
long input = 123L;
long negValue = -38L;
System.out.println(Math.decrementExact(value));
System.out.println(Math.decrementExact(input));
System.out.println(Math.decrementExact(negValue));
}
}

This will produce the following output:

20
122
-39

All contributors

Contribute to Docs

Learn Java on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours