java.lang.Math.negateExact()
Description
- public static int negateExact(int a)
- public static long negateExact(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 negateExact() 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.negateExact(a).
From the first part we have described this method to throw an exception if the result overflows the specified data type. If you are interested on the limit of the result before it throws an exception, you can find it using Integer.MIN_VALUE and Integer.MAX_VALUE if you are interested on int datatype. Meanwhile if you are interested on the range for long, you can find it using Long.MAX_VALUE and Long.MIN_VALUE.
With the complicated description above on this method functionality, you might start to wonder why not directly use negation (e.g multiply by -1). That might be the easiest solution though if we breach the limitation of data type the result would be inconsistent, not unlike if we use the negateExact() which has the capability to throw an exception if the result overflows of its range. So with this capability to throws an exception, will give us room to provide mechanism to handle such scenario. There would only be one scenario that i could think of that can result to overflow and that would be if our input is equal to the minimum value. Let’s take for an instance a data type int which has a minimum value of -2147483648 and maximum value of 2147483647. So if we negate -2147483648 the result would be 2147483648 which already beyond the maximum value.
Notes:
- ArithmeticException will be throw if the operation overflows the specified data type.
Method Syntax
public static int negateExact(int x, int y)
public static long negateExact(long x, long y)
Method Returns
The negateExact() method returns the product of the arguments.
Compatibility
Requires Java 1.8 and up
Java Math negateExact() Example
Below is a java code demonstrates the use of negatetExact() method of Math class. The example presented might be simple however it shows the behavior of the negateExact() method.
package com.javatutorialhq.java.examples; import java.util.Scanner; /* * This example source code demonstrates the use of * negateExact() method of Math class */ public class MathNegateExactExample { 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 int int value = Integer.parseInt(s); // get the result of negateExact int result = Math.negateExact(value); System.out.println("Result of the operation is " + result); System.out.println(Integer.MAX_VALUE); System.out.println(Integer.MIN_VALUE); } }
The above java example source code demonstrates the use of negateExact() 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 negateExact() method only accepts either int or long.