Java Program to Add Two Integers
In this java program, let's learn how to add two integer numbers in Java and display the result in the console window.
How to Add Two Integers in Java?
Sourcecode
/*
Author : CodersEditor.com
Program : Java Program to Add Two Integers
*/
public class Main {
public static void main(String[] args) {
int firstNumber = 7;
int secondNumber = 5;
System.out.println("First Number :" + firstNumber );
System.out.println("Second Number :" + secondNumber );
int result = firstNumber + secondNumber;
System.out.println("The sum is of two number is: " + result);
}
}
Output
First Number :7
Second Number :5
The sum is of two number is: 12
Explanation
- In this program, the two integer values 7 and 5 is stored in the variable firstNumber and secondNumber respectively.
- The two numbers are displayed on the console using System.out.println.
- The two numbers are added using the addition operator (+) and the result of the sum is stored in the variable called "result" and then displayed in the UI.
note
- The + operator is unique in the sense that when you use it with the string, it concatenates two string and when used for integers, it adds them.