Open In App

Java multiplyExact() in Math

Last Updated : 03 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The java.lang.Math.multiplyExact() is a built-in math function in java that returns the product of the arguments.It throws an exception if the result overflows an int. As multiplyExact(int a, int b) is static, so object creation is not required. 

Syntax:

public static int multiplyExact(int a, int b)
public static double multiplyExact(int a, double b)
Parameter :
 a : the first value
 b : the second value
Return :
This method returns the product of the arguments.
Exception :
It throws ArithmeticException - if the result overflows an int

Example: To show the working of java.lang.Math.multiplyExact() method. 

java




// Java program to demonstrate working
// of java.lang.Math.multiplyExact() method
 
import java.lang.Math;
 
class Gfg1 {
 
    // driver code
    public static void main(String args[])
    {
        int a = 25, b = 5;
        System.out.println(Math.multiplyExact(a, b));
 
        long c = 100, d = 50;
        System.out.println(Math.multiplyExact(c, d));
    }
}


Output:

125
5000

java




// Java program to demonstrate working
// of java.lang.Math.multiplyExact() method
 
import java.lang.Math;
 
class Gfg2 {
 
    // driver code
    public static void main(String args[])
    {
        int x = Integer.MAX_VALUE;
        int y = 2;
 
        System.out.println(Math.multiplyExact(x, y));
    }
}


Output:

Runtime Error :
Exception in thread "main" java.lang.ArithmeticException: integer overflow
    at java.lang.Math.multiplyExact(Math.java:867)
    at Gfg2.main(File.java:13)

Example:

Java




import java.io.*;
 
class GFG {
    // driver code
    public static void main(String[] args)
    {
        try {
            int x = Integer.MAX_VALUE;
            int y = 2;
            int result = Math.multiplyExact(x, y);
            System.out.println(x + " * " + y + " = "
                               + result);
        }
        catch (ArithmeticException e) {
            System.out.println(
                "Overflow occurred."); // print statement
        }
    }
}


Output:

Overflow occurred.


Similar Reads

StrictMath multiplyExact() Method in Java
The multiplyExact(int num1, int num2) is an inbuilt function of StrictMath class in java which is used to get the product of the given arguments. When the result overflows an int i.e. Integer.MAX_VALUE then it throws an exception. Since this function is static so there is no need to create objects. Syntax : public static int multiplyExact(int num1,
4 min read
java.lang.Math.atan2() in Java
atan2() is an inbuilt method in Java that is used to return the theta component from the polar coordinate. The atan2() method returns a numeric value between -[Tex]\pi [/Tex]and [Tex]\pi [/Tex]representing the angle [Tex]\theta [/Tex]of a (x, y) point and the positive x-axis. It is the counterclockwise angle, measured in radian, between the positiv
1 min read
Java.math.BigInteger.modInverse() method in Java
Prerequisite : BigInteger Basics The modInverse() method returns modular multiplicative inverse of this, mod m. This method throws an ArithmeticException if m <= 0 or this has no multiplicative inverse mod m (i.e., gcd(this, m) != 1). Syntax: public BigInteger modInverse(BigInteger m) Parameters: m - the modulus. Return Value: This method return
2 min read
Java.math.BigInteger.probablePrime() method in Java
Prerequisite : BigInteger Basics The probablePrime() method will return a Biginteger of bitLength bits which is prime. bitLength is provided as parameter to method probablePrime() and method will return a prime BigInteger of bitLength bits. The probability that a BigInteger returned by this method is composite and does not exceed 2^-100. Syntax: pu
2 min read
java.math.MathContext Class in Java
The java.math.MathContext class provides immutable objects that encapsulate context settings and define those numerical operator rules, such as those that the BigDecimal class implements. The base-independent configurations are as follows: Precision: The number of digits to be used for an operation; this accuracy is rounded to results.RoundingMode:
3 min read
Java.lang.Math Class in Java | Set 2
Java.lang.Math Class in Java | Set 1 More Methods: cosh() : java.lang.Math.cosh() method returns the hyperbolic cosine of the argument passed. Special cases : Result is NaN, if argument is NaN. Result is 1.0, if the argument is zero. Result is +ve infinity, if argument is infinite. Syntax: public static double cosh(double arg) Parameters: arg - The
6 min read
Java Math random() Method
The java.lang.Math.random() method returns a pseudorandom double type number greater than or equal to 0.0 and less than 1.0. When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression new java.util.Random. Declaration of Java Math random()Below is the declaration of java.lang.Math.random
2 min read
Java Math asin() method with Example
The java.lang.Math.asin() returns the arc sine of an angle in between -pi/2 and pi/2. Arc sine is also called as an inverse of a sine. If the argument is NaN or its absolute value is greater than 1, then the result is NaN. If the argument is zero, then the result is a zero with the same sign as the argument. Syntax : public static double asin(doubl
1 min read
Java Math min() method with Examples
The Java.lang.math.min() function is an inbuilt function in java that returns the minimum of two numbers. The arguments are taken in int, double, float and long. If a negative and a positive number is passed as an argument then the negative result is generated. And if both parameters passed are negative then the number with the higher magnitude is
2 min read
Java Math max() method with Examples
The Java.lang.math.max() function is an inbuilt function in Java which returns maximum of two numbers. The arguments are taken in int, double, float and long.If a negative and a positive number is passed as argument then the positive result is generated. And if both parameters passed are negative then the number with the lower magnitude is generate
2 min read
Article Tags :
Practice Tags :