Skip to main content

Matrix Multiplication Example in Java

3 min read Updated May 29, 2026
Share:
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

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. import java.util.Scanner; imports a class used later in the program.

  3. int mat1[][]=new int[3][3]; updates a variable used in the calculation or output.

  4. int mat2[][]=new int[3][3]; updates a variable used in the calculation or output.

  5. int mat3[][]=new int[3][3]; updates a variable used in the calculation or output.

  6. A println / print call writes text to the console — part of the sample output below.

  7. A Scanner reads typed input from the keyboard (System.in).

  8. 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 .java filename.
  • Forgetting semicolons at the end of statements.

Frequently Asked Questions

What does the Matrix Multiplication program demonstrate?
It shows how to implement matrix multiplication in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use these formulas in homework tools, engineering calculators or anywhere repeated numeric computation is needed.

Related Tutorials

Search tutorials