Matrix Multiplication in Java
In this lesson: Matrix multiplication program in Java using 2D arrays — logic, dry run and output. Copy & run now. In Hindi & English.
What is matrix multiplication
Matrix multiplication combines two matrices (2D arrays) into a new one. Each result cell is built by taking a row from the first matrix, a column from the second, multiplying matching elements, and adding them up. It is a classic exercise in nested loops and 2D arrays.
The multiplication rule
For 2×2 matrices A and B, the top-left result cell is: (A's first row) times (B's first column):
c[0][0] = a[0][0]*b[0][0] + a[0][1]*b[1][0]
You repeat this pattern for every result cell, each pairing one row of A with one column of B.
The Java program
public class MatrixMul {
public static void main(String[] args) {
int[][] a = {{1, 2}, {3, 4}};
int[][] b = {{5, 6}, {7, 8}};
int[][] c = new int[2][2];
for (int i = 0; i < 2; i++) { // each row of A
for (int j = 0; j < 2; j++) { // each col of B
for (int k = 0; k < 2; k++) { // sum the products
c[i][j] += a[i][k] * b[k][j];
}
}
}
for (int[] row : c) {
for (int x : row) System.out.print(x + " ");
System.out.println();
}
}
}
43 50
Three loops: i and j pick the result cell, and k adds up the products of A's row and B's column for that cell.
Dry run
| Result cell | Row × Column | Calculation | Value |
|---|---|---|---|
| c[0][0] | A row0 · B col0 | 1*5 + 2*7 = 5+14 | 19 |
| c[0][1] | A row0 · B col1 | 1*6 + 2*8 = 6+16 | 22 |
| c[1][0] | A row1 · B col0 | 3*5 + 4*7 = 15+28 | 43 |
| c[1][1] | A row1 · B col1 | 3*6 + 4*8 = 18+32 | 50 |
For each result cell, pair a row of A with a column of B, multiply matching elements, and add. c[0][0] uses A's first row and B's first column: 1*5 + 2*7 = 19.
When it is valid
Two matrices can be multiplied only if the columns of the first equal the rows of the second. An m×n matrix times an n×p matrix gives an m×p result. If the inner dimensions do not match, multiplication is not defined.
Common mistakes
- Not initialising the result. Result cells must start at 0 before adding products (Java arrays do this by default).
- Wrong index order. Use
a[i][k] * b[k][j]— the shared index isk. - Mismatched dimensions. Columns of A must equal rows of B.
- Expecting
A×B = B×A. Matrix multiplication is not commutative.
Summary
- Each result cell is a row of A dotted with a column of B.
- Use three nested loops: two for the cell, one to sum products.
- Valid only when A's columns equal B's rows.
- An m×n times n×p matrix gives an m×p result.
- Matrix multiplication is not commutative.
Next, practise 2D arrays in Java arrays, or nested loops in Floyd's triangle.
Frequently Asked Questions
How does matrix multiplication work?
When can two matrices be multiplied?
Why does matrix multiplication need three loops?
Is matrix multiplication commutative?
A × B is usually not equal to B × A for matrices. The order matters, and sometimes only one order is even valid based on the dimensions.How are matrices stored in Java?
int[][] a. You access an element with two indexes, a[row][col], and loop over rows and columns to process the whole matrix.