Java Program to Add Two Integers
To add two integers in Java, use the arithmetic addition operator +. The two integer values are the operands, and the value produced by the addition expression is their sum.
For example, if num1 contains 10 and num2 contains 25, the expression num1 + num2 evaluates to 35.
This tutorial covers two common ways to add integers in Java: adding values already stored in variables and reading two integers entered by the user with Scanner.
Java int Addition Syntax Using the + Operator
The basic syntax for storing the sum of two int values in another integer variable is:
int sum = num1 + num2;
Here, num1 and num2 are integer operands. Java evaluates the addition expression on the right side of = and assigns the result to sum.
Example 1 – Add Two Integer Variables in Java
In this example, the two integer values are written directly in the program. The + operator adds them, and the result is stored in sum.
AddTwoIntegers.java
/**
* Java Program - Add Two Integers
*/
public class AddTwoIntegers {
public static void main(String[] args) {
//two numbers
int num1 = 10;
int num2 = 25;
int sum;
//add two numbers
sum = num1 + num2;
//print the sum
System.out.println(sum);
}
}
How the Integer Addition Works
int num1 = 10; declares the integer variable num1 and initializes it with the value 10.
int num2 = 25; declares another integer variable, num2, and initializes it with 25.
int sum; declares an integer variable that will store the result.
In sum = num1 + num2;, Java first evaluates num1 + num2. With the values used in this example, the expression becomes 10 + 25, producing 35. That value is then assigned to sum.
System.out.println(sum); prints the value stored in sum to the console.
Run the program, and the following value is printed.
Output
35
Example 2 – Read Two Integers with Scanner and Find Their Sum
When the numbers are not known until the program runs, they can be read from standard input. The following program uses Scanner.nextInt() to read two integer values entered by the user and then adds them with the + operator.
AddTwoIntegers.java
import java.util.Scanner;
/**
* Java Program - Add Two Integers
*/
public class AddTwoIntegers {
public static void main(String[] args) {
//initialize scanner that scans input entered by user in console
Scanner myInput = new Scanner( System.in );
//read two numbers from console
System.out.print("Enter first number : ");
int num1 = myInput.nextInt();
System.out.print("Enter second number : ");
int num2 = myInput.nextInt();
int sum;
//add two numbers
sum = num1 + num2;
//print the sum
System.out.println(sum);
//close the scanner
myInput.close();
}
}
How Scanner Reads the Two Integers
Scanner myInput = new Scanner( System.in ); creates a Scanner that reads from Java’s standard input stream. In a console program, this normally means input typed with the keyboard.
System.out.print("Enter first number : "); displays a prompt asking the user for the first integer.
int num1 = myInput.nextInt(); reads the next integer token from the input and stores it in num1.
System.out.print("Enter second number : "); displays the prompt for the second value, and int num2 = myInput.nextInt(); reads that integer into num2.
The statement sum = num1 + num2; adds the two values entered by the user. The resulting integer is stored in sum.
System.out.println(sum); prints the calculated sum, and myInput.close(); closes the Scanner when input is no longer required.
Run the above Java program, and you shall get the following output.
)
Enter a number using keyboard and click enter (in Windows) or return (in Mac).
)
Enter a number using keyboard and click enter (in Windows) or return (in Mac).
)
Adding Negative and Zero Integer Values in Java
The same + operator works when either operand is negative or zero. For example, adding -8 and 3 produces -5, while adding 12 and 0 produces 12.
int num1 = -8;
int num2 = 3;
int sum = num1 + num2;
System.out.println(sum);
Output
-5
Java int Overflow When Adding Large Integers
A Java int is a 32-bit signed integer. If an addition produces a mathematical result outside the int range, ordinary + addition does not throw an exception; the result overflows and wraps into the int range.
When overflow must be detected instead of silently accepting the wrapped result, Java provides Math.addExact(). It returns the sum when the result fits in an int and throws ArithmeticException if overflow occurs.
int num1 = 100;
int num2 = 250;
int sum = Math.addExact(num1, num2);
System.out.println(sum);
Output
350
Java Integer Addition Summary
Use the + operator to add two int values in Java. The operands may be literals, variables, negative values, zero, or values read from user input. For console input, Scanner.nextInt() can read each integer before the addition is performed. If integer overflow needs to be detected, Math.addExact() can be used instead of ordinary + addition.
Continue with this Java Tutorial for more examples of Java operators, variables, input, and arithmetic operations.
TutorialKart.com