BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Math.multiplyExact() Method

By Chaitanya Singh | Filed Under: java

Java Math multiplyExact() method returns the product of its arguments. In this tutorial, we will discuss multiplyExact() method with examples.

public class JavaExample
{
  public static void main(String[] args)
  {
    int i = 20, i2 = 10;
    long l = 10000L, l2 = 15000L;
    System.out.println(Math.multiplyExact(i, i2));
    System.out.println(Math.multiplyExact(l, l2));
  }
}

Output:

200
150000000

Syntax of multiplyExact() method

Math.multiplyExact(10, 20); //returns 200

multiplyExact() Description

public static int multiplyExact(int x, int y): Returns the product of integer arguments x and y. It throws an ArithmeticException if the result overflows an int (which means if result is less than Integer.MIN_VALUE or greater than Integer.MAX_VALUE).

public static long multiplyExact(long x, long y): Returns the product of long arguments x and y. It throws an ArithmeticException if the result overflows a long (which means if result is less than Long.MIN_VALUE or greater than Long.MAX_VALUE).

public static long multiplyExact(long x, int y): This variation of multiplyExact() method is introduced in JDK 9 so it won’t run on older java versions. It returns the product of its arguments. It throws an ArithmeticException if the result overflows a long.

multiplyExact() Parameters

  • x: First argument
  • y: Second argument

multiplyExact() Return Value

  • Returns the product of x and y.

Example 1: Multiplication of two integer arguments

public class JavaExample
{
  public static void main(String[] args)
  {
    int x = 100, y = 3;
    System.out.println(Math.multiplyExact(x, y));
  }
}

Output:

Java Math.multiplyExact() Example Output_1

Example 2: Multiplication of two long arguments

public class JavaExample
{
  public static void main(String[] args)
  {
    long x = 1000L, y = 3000L;
    System.out.println(Math.multiplyExact(x, y));
  }
}

Output:

Java Math.multiplyExact() Example Output_2

Example 3: Integer overflow

public class JavaExample
{
  public static void main(String[] args)
  {
    int x = 100, y = Integer.MAX_VALUE;
    System.out.println(Math.multiplyExact(x, y));
  }
}

Output:

Java Math.multiplyExact() Example Output_3

Example 4: Long overflow

public class JavaExample
{
  public static void main(String[] args)
  {
    long x = 1000L, y = Long.MIN_VALUE;
    System.out.println(Math.multiplyExact(x, y));
  }
}

Output:

Java Math.multiplyExact() Example Output_4

Recommended Posts

  • Java subtractExact()
  • Java Math.addExact()
  • Java Math.nextDown()
  • Java Math.nextUp()
❮ Java Math

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap