×

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

Constructor Program in Java

Constructor Program in Java In Java, a constructor is a piece of code that is used to create an object. A constructor is called implicitly when an object is created in...

7 minutes read.

How to Calculate the Time Difference between Two Dates in Java?

The date is used extensively in Java to calculate date discrepancies. The date of joining an organization, admittance, appointment, etc., can be included when creating the application. The differences between...

4 minutes read.

Java Get File Size

There are three ways to get the file size in Java. Using Java InputOutput Using NewInputOutput Library Files.size() Method FileChannel.size() Method Using Apache Commons InputOutput Using Java InputOutput: The Java IO package offers the classes that deal...

5 minutes read.

Java String replaceAll() method

Java String replaceAll() method returns a String replacing all the sequence of characters matching regular expression i.e regex and replacement string. Syntax: public String replaceAll(String regex, String replacement) Parameters: regex : regular expression replacement :...

1 minute read.

Converting Long to Date in Java

What Long and Date are in Java and how are they implemented in the Java programming language are the topics of this article. Additionally, we'll go into great detail on...

4 minutes read.

Java File

Java file class implements the concept of file handling. It has several methods, such as deleting, creating, reading, and updating files. This class allows java users to perform various operations...

5 minutes read.

Java Math nextAfter() Method

The nextAfter() method of Math class returns the floating-point value adjacent to the first argument in direction of the second argument. Syntax: public static double nextAfter (double start, double direction)public static float...

2 minutes read.

Java Math.multiplyExact() method

In java, we are provided with multiplyExact method in the Math module. This Math module belongs to the java.lang package. In general, this method returns the product of the provided...

2 minutes read.

Java Math cos() Method

The cos() method of Math class returns the trigonometric cosine of the specified angle. Syntax: public static double cos(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The cos() method returns...

1 minute read.

Pattern Programs in Java

Pattern Programs in Java In Java, pattern programs are the most important from the perspective of interviews. The pattern programs improve thinking and coding skills. It also helps us to develop a...

27 minutes read.

Anonymous Function in Java

A function defined as being unbound from an identifier is called an anonymous function. Because they permit access to variables within the scope of the contained function, these are a...

4 minutes read.

Java Editors

A straightforward text editor may be used to create Java applications. However, a Java integrated programming environment (IDE) enables the software developer to create programs more quickly. An IDE offers...

4 minutes read.

ConcurrentSkipListSet in Java

ConcurrentSkipListSet is a class that is a part of collection framework in Java.It has been available since Java version 1.6. ConcurrentSkipListSet  is a scalable concurrent NavigableSet implementation based on a ConcurrentSkipListMap.The...

16 minutes read.

Java LinkedList vs ArrayList

LinkedList In LinkedList, each element is a distinct entity containing an information portion and an address component, and the elements are not kept in consecutive locations. Pointers & addresses are used...

3 minutes read.

Methods in Java

The Methods in Java are the collection of statements that are executed when the method is called. By using the methods, the complexity of writing the code decreases. The method consists...

4 minutes read.

Java Xmx

This section will explain what Xmx in Java is and how to establish a Java application's maximum heap size. When we execute a Java application, it occasionally displays an error message...

3 minutes read.

Java Math atan() Method

The atan() method of Math class computes the trigonometric Arc Tangent (inverse of tangent ) of an angle. The value returned is between -pi/2 to pi/2. Syntax: public static double atan(double a) Parameters: The...

2 minutes read.

Bucket Sort in Java

Bucket Sort in JavaBucket sort is also called bin sort. Bucket sort first puts the elements of the array or list into different buckets. The first bucket contains elements of the smallest value. The...

11 minutes read.

Iterate JSON array in Java

This article is going to equip the knowledge about what JSON array is and how it works and also how is it distinct with the generic array. Json Array Ordered lists of...

4 minutes read.

Kotlin Vs Java

Kotlin Vs Java There are many languages available for Android development. Java is the official language for android development but Kotlin is becoming popular nowadays. This article discusses both of these...

4 minutes read.