Java Math subtractExact(int a , int b) method
The java.lang.Math.subtractExact() is a built-in math function in java which returns the difference of the
arguments.It throws an exception if the result overflows an int.As subtractExact(int a, int b) is static, so
object creation is not required.
Syntax :
public static int subtractExact(int a, int b) Parameter : a : the first value b : the second value to be subtracted from the first Return : This method returns the difference of the arguments . Exception : It throws ArithmeticException - if the result overflows an int
Example :To show working of java.lang.Math.subtractExact() method.
// Java program to demonstrate working // of java.lang.Math.subtractExact() method import java.lang.Math; class Gfg1 { // driver code public static void main(String args[]) { int a = 300; int b = 200; System.out.println(Math.subtractExact(a, b)); } } |
chevron_right
filter_none
Output:
100
// Java program to demonstrate working // of java.lang.Math.subtractExact() method import java.lang.Math; class Gfg2 { // driver code public static void main(String args[]) { int x = Integer.MIN_VALUE; int y = 10; System.out.println(Math.subtractExact(x, y)); } } |
chevron_right
filter_none
Output:
Runtime Error :
Exception in thread "main" java.lang.ArithmeticException: integer overflow
at java.lang.Math.subtractExact(Math.java:829)
at Gfg2.main(File.java:13)
Recommended Posts:
- Java.math.BigInteger.modInverse() method in Java
- Java.math.BigInteger.probablePrime() method in Java
- Java Math exp() method with Example
- Java Math log() method with example
- Java Math min() method with Examples
- Java Math ulp() method with Examples
- Java Math cos() method with Examples
- Java Math tan() method with Examples
- Math scalb() Method in Java
- Java Math getExponent() method with Example
- Java Math expm1() method with Example
- Java Math round() method with Example
- Math floorMod() Method in Java
- Java Math nextAfter() method with Example
- Math floorDiv() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



