bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

Java Math negateExact() Method

Flash cards

Review the key moves

1/4
Core idea

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.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___.out.println(Math.negateExact(15));
3Order

Put the learning moves in the order that makes the concept easiest to apply.

The negateExact() method returns an integer with equal value and opposite sign to another integer and throws an exception if an overflow occurs.
Definition and Usage
Java Math negateExact() Method

❮ 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

ParameterDescription
xRequired. 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+

Previous

Java Math multiplyExact() Method

Next

Java Math nextAfter() Method