Java Math negateExact() Method Last Updated : 09 May, 2025 Comments Improve Suggest changes Like Article Like Report The java.lang.Math.negateExact() is a built-in function in Java that returns the negation of the argument, 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. Why does it Overflow?It throws an error when a data type int, which has a minimum value of -2147483648 and a maximum value of 2147483647. So, if we negate -2147483648, the result would be 2147483648, which is already beyond the maximum value.Examples:Input : 12Output : -12Input : -2Output : 2 Syntax of negateExact() Methodint Math.negateExact(int num)long Math.negateExact(long num)Parameter: num: The number we want to negate.Returns: It returns the value -num.Exception Throws: It throws an ArithmeticException if negating the value causes an overflow.Examples of Java Math negateExact() MethodExample 1: In this example, we will see the working of negateExact() method. Java // Java program to show how negateExact() works import java.lang.Math; public class Geeks { public static void main(String[] args) { int a = 10; int b = -12; System.out.println(Math.negateExact(a)); System.out.println(Math.negateExact(b)); } } Output-10 12 Explanation: Here in this example, the values 10 and -12 are negated to -10 and 12. Because the result are within the valid int range, that is why no exception is thrown.Example 2: The program below demonstrates the overflow of negateExact() method. Java // Java program to demonstrate overflow with negateExact() import java.lang.Math; public class Geeks { public static void main(String[] args) { int min = Integer.MIN_VALUE; System.out.println(Math.negateExact(min)); } } Output: Exception in thread "main" java.lang.ArithmeticException: integer overflowExplanation: Here in this example, the value Integer.MIN_VALUE is -2147483648. And we are trying to negate it. So, it results in a number that is outside the allowed range for integers. So, negateExact() throws an exception.Important Points:This method works for both int and long.It is safer than using the - operator when dealing with boundary values.This method throws an exception only in the case of overflow. Comment More infoAdvertise with us Next Article Java Math subtractExact(int a , int b) method gopaldave Follow Improve Article Tags : Misc Java Java-lang package java-math Practice Tags : JavaMisc Similar Reads Java Math negateExact() method The java.lang.Math.negateExact() is a built-in function in Java that returns the negation of the argument, 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. Why does it Overflow?It throws an er 2 min read Java Math subtractExact(int a , int b) method The subtractExact() is a built-in function in Java provided by the Math class. This method returns the difference of the arguments. It throws an exception if the result overflows an int or long data type. As subtractExact() is static, so object creation is not required.This method helps detect overf 2 min read Java Math subtractExact(long x, long y) 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 a long.As subtractExact(long x, long y) is static, so object creation is not required. Syntax : public static long subtractExact(long x, 1 min read Javascript Math sign() Method The Math.sign() is a built-in method in JavaScript and is used to know the sign of a number, indicating whether the number specified is negative or positive.Syntax:Math.sign(number)Parameters:This method accepts a single parameter number which represents the number whose sign you want to know. Retur 2 min read C# | Math.Abs() Method | Set - 2 C# | Math.Abs() Method | Set â 1 In C#, Abs() is a Math class method which is used to return the absolute value of a specified number. This method can be overload by passing the different type of parameters to it. There are total 7 methods in its overload list. Math.Abs(Decimal) Math.Abs(Double) Mat 3 min read C# | Math.Abs() Method | Set - 1 In C#, Abs() is a Math class method which is used to return the absolute value of a specified number. This method can be overload by passing the different type of parameters to it. Math.Abs(Decimal) Math.Abs(Double) Math.Abs(Int16) Math.Abs(Int32) Math.Abs(Int64) Math.Abs(SByte) Math.Abs(Single) Mat 4 min read BigInteger negate() Method in Java Prerequisite: BigInteger Basics The java.math.BigInteger.negate() method returns a BigInteger whose value is (- this). negate() method will change the signed bit of BigInteger. Syntax: public BigInteger negate() Parameters: The method does not accept any parameter. Return Value: The method returns t 1 min read Java Math exp() method with Example The Math.exp() method is a part of the java.lang.Math class. This method is used to calculate the value of Euler's number e raised to the power of a specified exponent. In this article, we are going to discuss how this method works for regular values and for special cases such as infinity and NaN.Sp 2 min read Java Math abs() method with Examples Absolute value refers to the positive value corresponding to the number passed as in arguments. Now geek you must be wondering what exactly it means so by this it is referred no matter what be it positive or negative number been passed for computation, the computation will occur over the positive co 3 min read java.math class and its methods | Set 1 Math class provides mathematical functions to perform basic numeric operations such as exponential, logarithm, square root, and trigonometric functions. cosh, sin, tan, abs, bitLength, multiply and many more. Implementations of the functions of Math class do not return bit-for-bit same results. Henc 4 min read Like