Quick tutorial for matrix multiplication in java and understanding multidimensions arrays with code example.

Before moving to Multidimensional arrays in java let’s talk about arrays in java. Arrays in Java are a collection of elements having the same data types. They are basically primitive data types.

What are Multidimensional Arrays?

let’s talk about multidimensional arrays in java.
Arrays in java can be of any dimension.

The syntax of a multidimensional array in java is shown below.

data_type[1st dimension][2nd dimension][]..[Nth dimension] array_name = new data_type[size1][size2]….[sizeN];

To get a clear view let’s create a 2dimensional array.

int X[][]=new int[2][2];

Here, int is the data type, X is the array variable, new allocates memory for the object.
[2][2] – first [2] tells us the number of rows and the second [2] tells us the number of columns. Here, we are creating an array of two rows and two columns. let us see how it looks like with the help of a diagram.

2D Array


[0][0], [0][1], [1][0] and [1][1]

These are index positions. To access the 1st-row 2nd element we write-


let us understand this topic with an example:
For Example: “Matrix Multiplication”. Above all matrix multiplication is of great use. Matrix multiplication is a good example of a multidimensional array. It’s time to see how to do it.

Matrix Multiplication in Java

After all matrix multiplication has certain rules.

Let’s get familiar with them.

Rule 1: Matrix Multiplication

The product of two matrices is possible if the number of columns in the first matrix equals the number of rows in the second matrix.

Rule 2: Matrix Multiplication

The resulting matrix product will have the same number of rows as the first matrix and the same number of columns as the second matrix.

Rule 3: Matrix Multiplication

We have to multiply each row element of one matrix with the column element of the other. Sum it with the multiplication of the respective row and column element in another matrix.

The image below will help in better understanding.

matrix multiplication in java

Formula for matrix multiplication

For example: if A is a matrix 2×3 and B is 3×2. resultant matrix C will be 2×2.

Let us see the code part. After all, coding is important.

Multiplying Matrices Java Code

Below is the code for matrix multiplication in java.

import java.util.Scanner;

class MatrixMul {
    public static void main(String args[]) {
        int r1, r2, c1, c2, i, j, k, sum;
        Scanner in = new Scanner(System.in);

        System.out.println("Enter the number of rows of matrix1");
        r1 = in.nextInt();

        System.out.println("Enter the number columns of matrix 1");
        c1 = in.nextInt();
        System.out.println("Enter the number of rows of matrix2");
        r2 = in.nextInt();

        System.out.println("Enter the number of columns of matrix 2");
        c2 = in.nextInt();

        if (c1 == r2) {

            int mat1[][] = new int[r1][c1];
            int mat2[][] = new int[r2][c2];
            int res[][] = new int[r1][c2];

            System.out.println("Enter the elements of matrix1");

            for (i = 0; i < r1; i++) {

                for (j = 0; j < c1; j++)
                    mat1[i][j] = in.nextInt();

            System.out.println("Enter the elements of matrix2");

            for (i = 0; i < r2; i++) {

                for (j = 0; j < c2; j++)
                    mat2[i][j] = in.nextInt();

            System.out.println("\n\noutput matrix:-");
            for (i = 0; i < r1; i++)

                for (j = 0; j < c2; j++) {
                    sum = 0;
                    for (k = 0; k < r2; k++) {
                        sum += mat1[i][k] * mat2[k][j];
                    res[i][j] = sum;
            for (i = 0; i < r1; i++) {
                for (j = 0; j < c2; j++)
                    System.out.print(res[i][j] + " ");

        } else
            System.out.print("multipication does not exist ");



Enter the number of rows of matrix1
Enter the number columns of matrix 1
Enter the number of rows of matrix2
Enter the number of columns of matrix 2
Enter the elements of matrix1
10 12
-3 6
Enter the elements of matrix2
2 5
9 -1

output matrix:-
128 38 
48 -21

Explanation of Matrix Multiplication Code:

Below is the explanation of matrix multiplication Java code which we already did.

Creating a class and taking inputs (line-1 to line-17)

In  line-3, we are creating a class MatrixMul.
Using Scanner class we are taking inputs of rows and columns of both matrices.
r1,c1 in line-5 are the row and column variable of matrix 1.
r2 and c2 in line-5 are the rows and column variable of matrix 2.

Checking rules for Multiply Matrices Java (line-19 to line-41)

In line-19 we are checking if columns of the first matrix and rows of the second matrix are equal or not.
If equal we are creating 3 matrices. As mentioned the matrices are mat1,mat2 and res.
res stores the result, whose dimensions are equal to the rows of the first matrix and columns of the second one.
After all, we insert values into the matrices.

Algorithm for matrix multiplication in java (line-41 to line-50)

Nested for loops starting at line-41 and ending at line-50 gives us a clear view of the matrix multiplication algorithm.
In line-47, storing the sum in the sum variable.

Printing resultant Matrix (line-51-61)

Finally, we are printing the resultant matrix res.
Nested for loops from line-51 to line-56 are used in printing result.
the print statement at line-58 executes if and only if the column of the first matrix does not match with rows of the second one.

In conclusion, We hope this Java tutorial helped you understand Multidimensional Arrays and how to do Matrix Multiplication in Java? Keep learning keep sharing. Follow us on Facebook and Instagram.