In this article, you will learn how to write a program for Matrix multiplication using Java. Matrix multiplication is an essential operation in linear algebra, used to combine two matrices to produce a new matrix. The resulting matrix’s dimensions are determined by the number of rows of the first matrix and the number of columns of the second matrix.

To perform matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the second matrix. If matrix A is of dimensions “m × n” (m rows and n columns), and matrix B is of dimensions “n × p” (n rows and p columns), then the resulting matrix C will be of dimensions “m × p” (m rows and p columns).

Here’s the formula for matrix multiplication between matrices A and B to produce matrix C:

C[i][j] = Σ(A[i][k] * B[k][j]) for k = 1 to n

In this formula, C[i][j] represents the element at the ith row and jth column of the resulting matrix C. The element is obtained by summing the products of corresponding elements from the ith row of matrix A and the jth column of matrix B, where k varies from 1 to n.

In Java, you can implement matrix multiplication using nested loops to iterate through the elements of both matrices and calculate the corresponding elements of the resulting matrix C.

Here’s a Java program to demonstrate matrix multiplication:

```
package com.javacodepoint.array.multidimensional;
public class MatrixMultiplication {
// method to multiply two matrices
public static int[][] multiplyMatrices(int[][] A, int[][] B) {
int m = A.length; // Number of rows in matrix A
int n = A[0].length; // Number of columns in matrix A
int p = B[0].length; // Number of columns in matrix B
int[][] result = new int[m][p];
for (int i = 0; i < m; i++) {
for (int j = 0; j < p; j++) {
for (int k = 0; k < n; k++) {
result[i][j] += A[i][k] * B[k][j];
}
}
}
return result;
}
// method to print given matrix
public static void printMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int value : row) {
System.out.print(value + " ");
}
System.out.println();
}
}
// main method
public static void main(String[] args) {
int[][] A = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
int[][] B = { { 7, 8, 9 }, { 10, 11, 12 } };
int[][] result = multiplyMatrices(A, B);
System.out.println("Matrix A:");
printMatrix(A);
System.out.println("\nMatrix B:");
printMatrix(B);
System.out.println("\nResulting Matrix (A * B):");
printMatrix(result);
}
}
```

**OUTPUT:**

Matrix A:

1 2

3 4

5 6

Matrix B:

7 8 9

10 11 12

Resulting Matrix (A * B):

27 30 33

61 68 75

95 106 117

**Explanation:**

**1. Method multiplyMatrices:**

- This method takes two 2D integer arrays
`A`

and`B`

as input, representing the two matrices to be multiplied. - The method calculates the dimensions of the matrices
`A`

,`B`

, and the resulting matrix`result`

. - It initializes the
`result`

matrix as a new 2D array with dimensions “m × p”, where “m” is the number of rows in matrix`A`

, and “p” is the number of columns in matrix`B`

.

**2. Nested Loops:**

- The program uses three nested loops to perform the matrix multiplication.
- The outer loop (indexed by
`i`

) iterates over the rows of matrix`A`

. - The middle loop (indexed by
`j`

) iterates over the columns of matrix`B`

. - The innermost loop (indexed by
`k`

) is used for the summation and iterates over the columns of matrix`A`

and the rows of matrix`B`

.

**3. Matrix Multiplication Formula:**

- For each element
`C[i][j]`

in the resulting matrix`result`

, the program calculates the sum of products using the formula:`C[i][j] = Σ(A[i][k] * B[k][j])`

for`k`

varying from 0 to the number of columns in`A`

(or the number of rows in`B`

).

**4. Printing the Matrices:**

- The program also includes a helper method
`printMatrix`

to print the elements of a given matrix. - In the
`main`

method, it first defines two matrices`A`

and`B`

. - The program then calls the
`multiplyMatrices`

method to perform the matrix multiplication and store the result in the`result`

matrix. - Finally, it prints the original matrices
`A`

and`B`

, along with the resulting matrix`result`

.

Learn also: **Subtract the Two Matrices**.