Matrix Multiplication Example in Java
On this page (9sections)
Introduction
Matrix Multiplication is a classic Java console program that demonstrates the concept with complete source code and sample output. Calculation programs apply formulas to solve geometry, statistics and numeric problems.
This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.
Definition
A matrix (plural matrices) is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns that is treated in certain prescribed ways. matrix multiplication is a binary operation that takes a pair of matrices, and produces another matrix. Numbers such as the real or complex numbers can be multiplied according to elementary arithmetic. if A is an n m matrix and B is an m p matrix, their matrix product AB is an n p matrix, in which the m entries across the rows of A are multiplied with the m entries down the columns of B.
Matrix Multiplication Example Program
import java.util.Scanner;
class MatrixMultiplication{
public static void main(String args[]){
int mat1[][]=new int[3][3];
int mat2[][]=new int[3][3];
int mat3[][]=new int[3][3];
System.out.println("Enter the first (3*3) matrix:");
Scanner input=new Scanner(System.in);
for(int i=0;i<3;i++){
for(int j=0;j<3;j++)}
mat1[i][j]=input.nextInt();
}
System.out.println("Enter the second (3*3) matrix:");
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
mat2[i][j]=input.nextInt();
}
System.out.println("The two matrices to be multiplied are as follows:");
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
mat3[i][j]=0;
for(int k=0;k<3;k++){
mat3[i][j]+=mat1[i][k]*mat2[k][j];
}
}
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(mat1[i][j]+"\t");
}
System.out.println("\n");
}
System.out.println("\n");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(mat2[i][j]+"\t");
}
System.out.println("\n");
}
System.out.println("\n");
System.out.println("The matrix after multiplication is as follows");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(mat3[i][j]+"\t");
}
System.out.println("\n");
}
}
}
Sample Output
Enter the first (3*3) matrix:
6
7
3
8
3
2
76
4
2
Enter the second (3*3) matrix:
0
3
7
2
5
1
2
7
8
The two matrices to be multiplied are as follows:
6 7 3
8 3 2
76 4 2
0 3 7
2 5 1
2 7 8
The matrix after multiplication is as follows
20 74 73
10 53 75
12 262 552
When to use
Use these formulas in homework tools, engineering calculators or anywhere repeated numeric computation is needed.
How it works
-
Execution begins in the
mainmethod — the JVM calls this method when you run the class. -
import java.util.Scanner;imports a class used later in the program. -
int mat1[][]=new int[3][3];updates a variable used in the calculation or output. -
int mat2[][]=new int[3][3];updates a variable used in the calculation or output. -
int mat3[][]=new int[3][3];updates a variable used in the calculation or output. -
A
println/printcall writes text to the console — part of the sample output below. -
A
Scannerreads typed input from the keyboard (System.in). -
A loop repeats the block until its condition becomes false.
Best Practices
- Use meaningful variable and class names that describe their purpose.
- Compile and run the program locally — modify values to see how output changes.
- Read compiler errors carefully; they usually point to the exact line to fix.
Common Mistakes
- Copying code without understanding each line — practice by changing one statement at a time.
- Mismatching the public class name and the
.javafilename. - Forgetting semicolons at the end of statements.