Java .negateExact()

YanisaHS's avatar
Published Oct 31, 2022
Contribute to Docs

The Math.negateExact() method returns the negation of the argument and raises an exception if the result overflows.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours

Syntax

Math.negateExact(num)

The num parameter can be of type int or long.

Example

The following example demonstrates using the .negateExact() method:

// Test.java
public class Test {
public static void main(String args[]) {
int num_int = 12;
long num_long = 123L;
System.out.println(Math.negateExact(num_int));
System.out.println(Math.negateExact(num_long));
}
}

This results in the following output:

-12
-123

All contributors

Contribute to Docs

Learn Java on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours