×

Java Math negateExact() Method

The negateExact() method of Math class returns the negation for the specified argument, throwing an exception if the result overflows an int or a long.

Syntax:

  1. public static int negateExact (int a)
  2. public static long negateExact (long a)

Parameters:

The parameter ‘a’ represents the value to negate.

Return Value:

The negateExact () method returns the negation of a.

Throws:

The negateExact () method throws:

ArithmeticException- if the result overflows an int or long.

Example 1:

public class JavaMathNegateExactExample1 {
    public static void main(String[] args) {
        int a = 34;
        //returns the negation for a
        System.out.println("Negation of "+a+" is : "+Math.negateExact(a));
    }
}

Output:

Negation of 34 is : -34

Example 2:

public class JavaMathNegateExactExample2 {
    public static void main(String[] args) {
        long a = -8765;
        //returns positive value for negative a
        System.out.println("Negation of "+a+" is : "+Math.negateExact(a));
    }
}

Output:

Negation of -8765 is : 8765

Example 3:

public class JavaMathNegateExactExample3 {
    public static void main(String[] args) {
        Long a = 566l/0l;
        //throws exception if a is divided by zero
        System.out.println("Negation of "+a+" is : "+Math.negateExact(a));
    }
}

Output:

Exception in thread "main" java.lang.ArithmeticException: / by zero
         at com.TutorialAndExamples.JavaMathNegateExactExample3.main(JavaMathNegateExactExample
3.java:5)

Example 4:

public class JavaMathNegateExactExample4 {
    public static void main(String[] args) {
        Long a = Long.MIN_VALUE;
        //throws wxception if a is divided by zero
        System.out.println("Negation of "+a+" is : "+Math.negateExact(a));
    }
}

Output:

Exception in thread "main" java.lang.ArithmeticException: long overflow
         at java.lang.Math.negateExact(Math.java:994)
         at com.TutorialAndExamples.JavaMathNegateExactExample4.main(JavaMathNegateExactExample
4.java:7)

Related Topics

Interface in Java

  Interface in Java In Java, the interface is just like a class that has only static constants and abstract methods. It is used to achieve polymorphism so that it can also...

6 minutes read.

Java Set Interface

We use set when we don't want to allow duplicate entries. All Set implementations do not allow duplicates. HashSet: This class stores its elements in hash tables. It uses the hashCode()...

3 minutes read.

Java 9 Try With Resources

Java 9 provides the improvement in the try statement. It allows us to declare a try statement with duly declared resources. Whenever the user does not require the functionality with...

3 minutes read.

How to Run Applet Program in Java

An applet is a new type of program which is put in a webpage. By inserting applet into a webpage dynamic content can be created. Characteristics of an Applet The applet is...

11 minutes read.

Memory Leak in Java

Java offers memory management right out of the box. When we use the new keyword to create an object, the JVM initialises for that object immediately. The trash collector automatically...

3 minutes read.

GCD Program in Java

GCD Program in Java The GCD program in Java outputs the GCD of the given numbers. In mathematics, Greatest Common Divisor (GCD), Greatest Common Factor or Highest Common Factor (HCF) of...

14 minutes read.

Java Volatile Keyword

The compiler, runtime, or processors may use any kind of optimization if there aren't any required synchronizations. Although most of the time these improvements are advantageous, they occasionally can result...

6 minutes read.

Java Future Example

Future is an interface in the Java language that is a part of  java.util.concurrent package. It serves as a symbol for the output of an asynchronous computation. The interface offers ways to determine whether a computation has finished,  wait for it to finish, and receive its result. Once the task or calculation is finished, it cannot be undone. A Future interface offers ways to determine whether the computation is finished, to wait for it to finish, and to receive the computation's results. When the computation...

3 minutes read.

Java Math hypot() Method

The hypot() method of Math class returns the square root for the expression x2 + y2 without the intermediate underflow or overflow . Syntax: public static double hypot(double x, double y) Parameters: The parameters...

2 minutes read.

Narcissistic Number in Java

A Narcissistic number is made up of digits that have been added together and raised to powers equal to the number of digits in the original number. In those other...

3 minutes read.

Morris Traversal for Inorder in Java

Through Morris’s traversal, a tree is traversed without the aid of recursion or stacks. Based on the threaded binary tree, the Morris traversal is used. We perform internal modification throughout...

4 minutes read.

Java Integer toUnsignedString() method

The toUnsignedString() method of Java Integer class returns a string representation of the argument as an unsigned decimal value. The second syntax returns a string representation of the given argument as...

2 minutes read.

Java Math pow() Method

The pow() method of Math class returns the value of first argument raised to the power of second argument i.e. ab. Syntax: public static double pow(double a, double b) Parameters: The parameters ‘a’ and...

3 minutes read.

Java Virtual Machine (JVM)

JVM is a virtual runtime environment to execute Java byte codes. The JVM doesn’t understand the keywords we used to write code. That is why it is converted into bytecode. It controls the...

3 minutes read.

POJO in Java

Plain old Java Object, in short, is called POJO in Java. POJO is an everyday object that is not subject to any specific limitations. We can use POJO in any Java...

3 minutes read.

Convert JSON File to String in Java

Before understanding the conversion of JSON file to string, one must know about JSON. What is JSON? JSON stands for JavaScript Object Notation. It is an open standard format lightweight, text-based, and...

3 minutes read.

Sliding Window Problem in Java

A sliding window is used in computer science and data science to process large datasets. It involves breaking the dataset into smaller chunks or windows and then processing it in...

6 minutes read.

Java Variable

The variable is the basic unit of storage in a program. We define a variable using an identifier, a type, and an optional initializer in Java. In Java, variables must be...

4 minutes read.

Java Flags Enum

In a programming language, enumerations represent a group of named constants.For instance; the four suits in a deck of playing cards could be the enumerators Club, Diamond, Heart, and Spade,...

3 minutes read.

How to Convert Decimal to Hexadecimal in Java

How to Convert Decimal to Hexadecimal in Java The hexadecimal number uses 16 values to represent a number. Numbers from 0 to 9 represented by digits and the numbers from 10...

3 minutes read.