Safely Increment Values with incrementExact() Method in Java: A Complete Guide

Rumman Ansari   Software Engineer   2024-07-04 04:25:29   6190  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 incrementExact() method of Math Class. The incrementExact() returns the argument incremented 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 incrementExact() method is overloaded. Below are the two overloaded method of the decrementExact() method: 

  • public static int incrementExact(int a)
  • public static long incrementExact(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 incrementExact() 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.incrementExact(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 increment, the only scenario that we will be hitting an exception if the result is greater than the maximum value. The maximum value can be derived from Long.MAX_VALUE or Integer.MAX_VALUE.

Method Syntax

public static int incrementExact(int a)

public static long incrementExact(long a)

Method Returns

The incrementExact() method returns the results.

Compatibility

Requires Java 1.8 and up

Example

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



import java.util.Scanner;

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

public class MathIncrementExact {

	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 incrementExact
		int result = Math.incrementExact(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:12
Result of the operation is 13
Press any key to continue . . .

The above java example source code demonstrates the use of incrementExact() 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 incrementExact() method only accepts either int or long.