Flash cards
Review the key moves
What is the main idea behind Java Math negateExact() Method?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
Which statement best captures the main point of this lesson?
Complete the missing token from the example code.
___.out.println(Math.negateExact(15));Put the learning moves in the order that makes the concept easiest to apply.
❮ Math Methods
Example
System.out.println(Math.negateExact(15));
System.out.println(Math.negateExact(-32));
System.out.println(Math.negateExact(7));
System.out.println(Math.negateExact(-25));Definition and Usage
The negateExact() method returns an integer with equal value and opposite sign to another integer and throws an exception if an overflow occurs. This prevents incorrect results that can occur from the overflow.
An overflow occurs when the integer is equal to Integer.MIN_VALUE or Long.MIN_VALUE , the largest negative integer, because it does not have a positive equivalent.
One of the following
public static int negateExact(int
x )public static long negateExact(long
x )Parameter Values
| Parameter | Description |
|---|---|
| x | Required. An integer to negate. |
Technical Details
| Returns: | An int or long value representing an integer with equal value and opposite sign to another integer. |
|---|---|
| Throws: | ArithmeticException - If the negation causes an overflow. |
| Java version: | 1.8+ |