Java Program to Multiply Two Matrices - Matrix Multiplication Example (original) (raw)

How to write a Java program to multiply two matrices in Java is a very good programming exercise to get familiar with the two-dimensional array in Java. this example teaches about how to multiply arrays, how to access elements from a multi-dimensional array, how to pass them to a function etc. Since the matrix is a natural representation of a multi-dimensional array in Java, they are often used to illustrate real word matrix exercises e.g. the calculating sum of two matrices or calculating the difference of two matrices, etc. It's also one of the popular Matrix based coding problems you can find on coding interviews.

By the way, before writing the program, let's recap how to multiply two matrices in mathematics first. If you remember, you can only multiply two matrices if, and only if, the number of columns in the first matrix equals the number of rows in the second matrix. That is known as the matrix multiplication criterion.

If both matrices don't satisfy that criterion then the product of two matrices is undefined. The Product matrix's dimensions will be equal to (rows of the first matrix) × (columns of the second matrix ).

For example, if we multiply a 2×3 matrix with a 3×1 matrix, then the product matrix or result matrix will be a 2×1 matrix i.e. two rows and 1 column.

I used to remember this trick by writing dimensions of matrices adjacent to each other and canceling their matching dimension e.g. if you write 2x3 and 3x1, and then cancel 3 from each side you will get a matrix of dimension 2x1, which is basically the dimension of the product matrix.

How to multiply two matrices in Java? Example

Here is a graphical representation of the matrix multiplication algorithm:

Java Program to Multiply Two Matrices - Matrix Multiplication Example

You can see that both matrices met the condition for multiplication i.e. columns of the first matrix are equal to rows of the second matrix. Then we multiply the first row of the first matrix to the first column of the second matrix and this gives us the first element of the first column of result matrix.

Similarly, when you multiply the second row of the first matrix to the first column of the second matrix you get the second element of the first column in the result matrix.

How to multiply two matrices in Java- Example

Here is our complete Java program to perform matrix multiplication. In this program, we first ask the user to enter two matrices. Since you cannot accept an array from the command line in Java (see here), we ask the user to first enter the number of rows and columns of the matrix and then ask him to populate the matrix.

Once we have both the matrices ready we first check whether they met the condition of matrix multiplication or not i.e. number of columns of the first matrix matches to the rows of the second matrix.

As I said, we have used a two-dimensional array to represent a matrix in Java.

import java.util.Scanner;

/**

}

and here is the output of this program when you will run this on your favorite Java IDE e.g. Eclipse or IntelliJ IDEA or just from the command prompt:

Output: Enter the number of rows and columns of the first matrix 2 3 Enter the elements of the first matrix 1 2 3 4 5 6 Enter the number of rows and columns of the second matrix 2 4 Matrices with entered orders can't be multiplied with each other, columnsOfFirstMatrix [3] != rowsOfSecondMatrix [2] Enter the number of rows and columns of the second matrix 3 2 Enter numbers of the second matrix 7 8 9 10 11 12 The product of entered matrices:- 58 64 139 154

You can see that our first example was not perfect, the matrices we entered cannot be multiplied with each other because columns of the first matrix are not equal to rows of the second matrix.

That's all about how to do matrix multiplication in Java. This is a good programming exercise to learn and understand how to use two-dimensional arrays in Java, which is one of the key data structures, especially for the game development field.

If you are just starting with programming and not familiar with key programming concepts then I also suggest you join these interactive programming courses which teach you the basics of programming in Java and other programming languages.

Other Java Programming exercises for beginners

Thanks a lot for reading this article so far. If you find the solution of Matrix multiplication in Java and understand concept better then please share this Java tutorial with your friends and colleagues. If you have any questions feel free to ask in comments.

P. S. - If you are new to Java and looking for free online courses and tutorials to learn Java then you can also checkout this list of 5 best Free Core Java Courses for Beginners. This list contains best free courses to learn Java online from Udemy and Coursera.

And lastly one question for you? Which one is your favorite matrix based coding problem? addition of matrix, multiplication, or transpose of matrix?