Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

incrementexact
java
math
communitycreator

What is Math.incrementExact() in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The incrementExact method in Java increments the value passed as an argument by 1, and returns the incremented value. The value stored in the passed variable is unchanged.

The arguments will be either int or long. incrementExact is a static method present in the Math class.

Syntax

public static long incrementExact(long a);

public static int incrementExact(int a);

Arguments

  • a: Value to increment (int/long).

Return value

This function returns the incremented value of a, i.e, a+1.

Example

import java.lang.Math;
class IncrementExact {
public static void main(String cmdArgs[]) {
int a = 10;
int incrementedValue = Math.incrementExact(a);
System.out.print("The increamented value of "+ a + " is ");
System.out.println(incrementedValue);
long la = 100000000l;
long lIncrementedValue = Math.incrementExact(la);
System.out.print("The increamented value of "+ la + " is ");
System.out.println(lIncrementedValue);
}
}

In the code above:

  • We created two variables: a of the int type and la of the long type.

  • We called the Math.incrementExact method with a as arguments. This will return the increment of the value of a by 1. In our case, this is the value of a=10, so incrementExact will return 11. The value of a remains unchanged as 10.

  • Again, we called the Math.incrementExact method with la as an argument; this will return the increment of the value of la by 1.


The incrementExact method will throw an ArithmeticException if:

  • The argument is int, and the increment of the argument overflows the int value.

  • The argument is long, and the increment overflows the long value.

Example

For example, consider if the argument is int. The ArithmeticException will be thrown if the increment of the int argument is less than the minimum value or greater than the maximum value that the int type can hold.

import java.lang.Math;
class IncrementExactTest {
public static void main(String cmdArgs[]) {
try{
int a = Integer.MAX_VALUE;
int incrementedVal = Math.incrementExact(a);
}catch(Exception e){
System.out.println(e);
}
}
}

In the code above, we created a variable, a of the int type. We assigned the maximum value an integer can hold to the a variable.

When we call the incrementExact method with a as an argument, we will get ArithmeticException. This is because when we increment the value of a, the value will be greater than the maximum value an integer can hold.

RELATED TAGS

incrementexact
java
math
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring