Multiplying in Java: Method & Examples

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Multiplying Matrices in Java

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
Your next lesson will play in 10 seconds
  • 0:04 Multiplying in Java
  • 0:29 Combining Statements
  • 1:35 Overflow!
  • 2:45 Double/Float/Long
  • 3:38 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Java provides multiple tools for arithmetic operations. This lesson will discuss multiplication, provide the methods used for this operation, as well as working code examples.

Multiplying in Java

Java provides several arithmetic operations that you can use in your programs. The language supports statements from the very simple to the incredibly complex. In this lesson, we will start with the basics and highlight more complicated routines.

For review, here are the Java operators for arithmetic:

Operator Use
* Multiplication
/ Division
+ Addition
- Subtraction
% Modulo/remainder

In order to multiply numbers in Java, we will use the asterisk (*) between each number or variable.

int x = 12;
int y = 13;
int z = x * y;
System.out.println("Multiplication: " + z);

The output is listed below:

Java multiply sample output

Combining Statements

Java fully supports nested statements and the combination of arithmetic operations. Keep in mind that the order of operations rule applies!

Multiplication and division are processed before addition/subtraction.

We recommend you desk-check your calculations, especially as they get more complex. Plug in some sample values and do the math long-hand, ensuring that your result matches what Java calculates.

In checking the first equation, we would have the following: 3 + (6 * 9). Multiplication comes first. Therefore, 6 * 9 is 54; then we add 3 to get 57. The next equation moves the parentheses around, but it also changes the math. (3 + 6) * 9. There is only one number being multiplied, so we take 9 times the result of 3 + 6 (9). The result is 81.

Here is the Java code:

int x = 3;
int y = 6;
int z = 9;
//variation 1
int q = x + (y * z);
System.out.println("Result 1 = " + q);
variation 2
q = (x + y) * z;
System.out.println("Result 2 = " + q);

After running the program, our calculations are correct.

Java multiplication order of operations output

So far we've been showing you integer multiplication. What happens if the numbers get too big for int? One option is to use float or double to do your math. There is also another option for int or long.


Because we are multiplying numbers, there is a risk that the resulting number will be larger than our data type can hold. Even though int and long data types can hold some fairly large numbers, once you start multiplying, the chances of overflow increase dramatically.

Java provides a core class called Math; with this class are several methods. One of these is multiplyExact. It accepts two values, either int or long. If Java overflows, it throws an error we can deal with. Since we place the whole thing within a try and catch block, we can catch that error and do something, versus letting the program crash and burn. Or worse: Java actually tries the math, but the result is just meaningless numbers.

Here is an example of the multiplyExact in action. Again, this is fairly advanced, but it is good to have an awareness of the various methods available for arithmetic.

int x = 125025994;
int y = 100547903;
int z = x * y;
//let's try multiplyexact; if it fails, show a message
try {   int newResult = Math.multiplyExact(x,y);
  System.out.println("Fixed: " + newResult);
} catch (ArithmeticException a) {

In this example, the code will overflow and the message will be displayed:

Java multiplyExact output

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account