English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java flow control

Java array

Java object-oriented (I)

Java object-oriented (II)

Java object-oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map collection

Java Set collection

Java input and output (I/O)

Java Reader/Writer

Java other topics

Java program adds two integers

Java Examples Comprehensive

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.

Example: Program to add two integers

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, use + 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.

Java Examples Comprehensive