Java Program to find sum of two integer numbers

In this program let us create a Java program to print the sum of two numbers on the console.

Program

public class AddNumbers {
    public static void main(String args[]) {
        int a, b;
        a = 10;
        b = 32;
        int sum = a + b;
        System.out.println("Sum of " + a + " and " + b + ":\t" + sum);
    }
}

Let us understand the program by breaking into simple parts:

  1. public class AddNumbers: Any Java program starts with a class, in this case the class name is AddNumbers
  2. public static void main(String[] args): main is the entry point for any Java program.
  3. int a, b; : Declare variables a and b of type integer
  4. a = 10; : Initiable variable a to 10
  5. b = 32; : Initiable variable b to 32
  6. int sum = a + b; : Calculate the sum of a and b and store it in variable sum.
  7. System.out.println("Sum of " + a + " and " + b + ":\t" + sum);: System.out.println prints the value of a and b along with the sum.

Output

Sum of 10 and 32:	42