How to perform Matrix Subtraction In Java?

 Below program will subtract two matrix and return the sum in third matrix:

package testjava.arrays;

import java.util.Scanner;

public class MatrixSubtraction
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Enter The Number Of Rows");
		int row = sc.nextInt();
		
		System.out.println("Enter The Number Of Columns");
		int cols = sc.nextInt();
		
		int[][] matrix1 = new int[row][cols];
		int[][] matrix2 = new int[row][cols];
		int[][] matrixSub = new int[row][cols];
		
		System.out.println("Enter The Data For First Matrix :");
		for (int i = 0; i < row; i++)
		{
			for (int j = 0; j < cols; j++)
			{
				matrix1[i][j] = sc.nextInt();
			}
		}
		
		System.out.println("Enter The Data For Second Matrix :");
		for (int i = 0; i < row; i++)
		{
			for (int j = 0; j < cols; j++)
			{
				matrix2[i][j] = sc.nextInt();
			}
		}
		
		System.out.println("---First Matrix---");
		for (int i = 0; i < row; i++)
		{
			for (int j = 0; j < cols; j++)
			{
				System.out.print(matrix1[i][j]+"\t");
			}
			System.out.println();
		}
		
		System.out.println("---Second Matrix---");
		for (int i = 0; i < row; i++)
		{
			for (int j = 0; j < cols; j++)
			{
				System.out.print(matrix2[i][j]+"\t");
			}
			System.out.println();
		}
		
		System.out.println("---Subtraction of Matrix---");
		for (int i = 0; i < row; i++)
		{
			for (int j = 0; j < cols; j++)
			{
				matrixSub[i][j] = matrix1[i][j] - matrix2[i][j];
				System.out.print(matrixSub[i][j]+"\t");
			}
			System.out.println();
		}
	}
}
Output
Enter The Number Of Rows
3
Enter The Number Of Columns
3
Enter The Data For First Matrix :
9
8
7
6
5
4
3
2
1
Enter The Data For Second Matrix :
3
3
3
3
3
3
3
3
3
---First Matrix---
9	8	7	
6	5	4	
3	2	1	
---Second Matrix---
3	3	3	
3	3	3	
3	3	3	
---Subtraction of Matrix---
6	5	4	
3	2	1	
0	-1	-2