Safely Decrement Values with decrementExact() Method in Java: A Complete Guide

Rumman Ansari   Software Engineer   2024-07-04 04:22:50   6036  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Description

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.

Compatibility

Requires Java 1.8 and up

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.



import java.util.Scanner;

/*
 * This example source code demonstrates the use of
 * decrementExact() method of Math class
 */

public class MathDecrementExact {

	public static void main(String[] args) {

		// Ask for user input
		System.out.print("Enter an input:");

		// use scanner to read the console input
		Scanner scan = new Scanner(System.in);

		// Assign the user to String variable
		String s = scan.nextLine();

		// close the scanner object
		scan.close();

		// convert the string input to double
		int value = Integer.parseInt(s);

		// get the result of decrementExact
		int result = Math.decrementExact(value);
		System.out.println("Result of the operation is " + result);

	}

}

output

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

Enter an input:135
Result of the operation is 134
Press any key to continue . . .


Enter an input:-16
Result of the operation is -17
Press any key to continue . . .

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.