English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this program, you will learn how to store and add two integers in Java. After addition, the final sum will be displayed on the screen.
public class AddTwoIntegers { public static void main(String[] args) { int first = 10; int second = 20; System.out.println("Enter two numbers: " + first + " + second); int sum = first + second; System.out.println("Sum: " + sum); } }
When running this program, the output is:
Enter two numbers: 10 20 Sum: 30
In this program, two integers10and20 to store in integer variables first and second.
Then, using + The operator adds first and second and stores the result in another variable sum.
Finally, use the println() function to print the sum on the screen.