| by Arround The Web | No comments

How to multiply in Java

The multiplication operator * and the “multiplyExact()” method can be used to multiply two values in java. The multiplication operator performs multiplication on any numeric value such as int, float, or double. The multiplyExact() method deals with only integer and double type values. It takes two values, performs multiplication on them, and returns the resultant value. However, if the resultant value exceeds the limit/range, it throws an exception.

In this post, we will learn how to multiply in java, and in this regard, we will cover the below-listed concepts:

So, let’s get started!

What are arithmetic operators in Java?

Java offers a set of arithmetic operators to perform different arithmetic tasks like multiplication, division, addition, etc.

Here is a list of arithmetic operators that can be used in java for different purposes:

Operator Functionality
* Used to perform multiplication.
+ The addition operator is used to add different values.
/ Performs division.
Performs subtraction.
% Returns remainder.

As this post aims to describe how to multiply in java, so, we will utilize the * operator in all the examples to perform multiplication.

How to multiply two numbers in Java?

Let’s consider some examples to learn how to multiply two numbers in java:

Example:1 How to multiply two integers?

int value1 = 150;
int value2 = 250;
int product = value1 * value2;
System.out.println("Resultant output: " + product);
  • Initially, we created two integers and initialized them with 150 and 250 respectively.
  • Next, we created another variable named “product” to store the multiplication result.
  • Finally, we printed the resultant value using the “System.out.println()” statement:

This is how you can find the product of two integers.

Example:2 How to multiply two floating-point numbers?

float value1 = 14.72f;
float value2 = 12.55f;
float product = value1 * value2;
System.out.println("Resultant output: " + product);

Here, in this example program:

  • We have created two variables i.e. “value1” and “value2” and assigned them some floating point values.
  • Next, we created another variable to store the product of “value1” and “value2”.
  • Finally, we utilized the println() method to show the resultant output:

The output authenticates that the multiplication operator * works perfectly fine on the floating-point values.

Example:3 How to multiply two doubles?

double value1 = 155.72;
double value2 = 350.50;
double product = value1 * value2;
System.out.println("Resultant output: " + product);
  • Firstly, we created two double type variables and assigned them 155.72 and 350.50 respectively.
  • Next, we created another variable named “product” to store the multiplication result.
  • We utilized the * operator between two variables to perform the multiplication.
  • Finally, we printed the resultant value using the “System.out.println()” statement.

In this way, you can multiply the double values in java.

What does multiplication overflow mean in Java?

In programming languages including java, each data type has some specific range. However, while multiplication, there is always a chance that the resultant value exceeds that range. In Java, such a situation is referred to as the multiplication overflow.

Example:1 How multiplication overflow occurs:

Example:1 How multiplication overflow occurs:
int value1 = 987654321;
int value2 = 987654321;
int product = value1 * value2;
System.out.println("Resultant output: " + product);
  • In this program, we have created two integer values that are within the range of int data types (i.e. -2,147,483,648 to 2,147,483,647).
  • Next, we multiplied both the values and stored the result in a variable named “product”.
  • The actual resultant value after multiplication should be “975,461,057,789,971,041”.

Let’s see what the output says when we run the above-given program:

On successful execution of the program we got a surprising output i.e. “-238269855”. This is because the maximum range for the integer data type is “2,147,483,647” however, the resultant value is “975,461,057,789,971,041” which is much more than the range of integer data type. Therefore we got an unusual value i.e. “-238269855”.

How to fix multiplication overflow in Java?

The java math class offers a wide range of built-in methods. The multiplyExact() method is one of them. It can accept either double or int values. In Java, the multiplyExact() method is used to perform the multiplication on two values. If an out-of-range value occurs then it will throw an exception (instead of showing a misleading result).

Example:2 how to use multiplyExact() method in Java

int value1 = 987654321;
int value2 = 987654321;
   try {
        int product = Math.multiplyExact(value1, value2);
        System.out.println("Resultant output: " + product);
        }
   catch (ArithmeticException excep) {
        System.out.println("Multiplication Overflow occurs!");
        }
  • We created two integers i.e. value1, and value2.
  • Next, we assigned them some values.
  • Afterward, we utilized the try-catch block to handle the exceptions.
  • Within the try-block, we utilized the multiplyExact() method to get the product of two values.
  • The catch block will get executed only if the resultant value is out-of-range.

This is how the Math.multiplyExact() method works in Java.

Conclusion

Java offers a couple of ways to perform multiplication such as the multiplication operator “*” and a built-in method multiplyExact(). The multiplication operator performs multiplication on any numeric value such as int, float, or double. The multiplyExact() method deals with only integer and double type values. The benefit of using the multiplyExact() method is that it throws an exception if the resultant value exceeds the limit/range.

Share Button

Source: linuxhint.com

Leave a Reply