Java multiplyExact() in Math

JavaObject Oriented ProgrammingProgramming

It is a built-in function in Java, that is used to multiply the two values that are passed as arguments to the function. Here’s an example −


 Live Demo

import java.lang.Math;
public class Demo{
   public static void main(String args[]){
      int a = 12, b = 34;
      System.out.printf("Product is : ");
      System.out.println(Math.multiplyExact(a, b));
      long c = 78, d = 93;
      System.out.printf("Product is : ");
      System.out.println(Math.multiplyExact(c, d));


Product is : 408
Product is : 7254

A class named Demo contains the main function. Here, two variables are defined, and their product is calculated using the ‘multiplyExact’ function. Now two long integers are defined and again, their product is computed using the ‘multiplyExact’ function. Next, they are printed on the console.

Published on 04-Jul-2020 09:17:30