Adding two numbers often seems to be too easy. What if I told you there are different ways through which a simple addition program could be performed in Java? Different Java programs for the addition of two numbers in Java are explained in simple language in the article; we have also discussed different ways through which users can provide input and much more.
Addition uses the "+" operator for adding two numbers.
Let us now look at the ways in which we can add two numbers in Java.
Method 1: Sum of Two Numbers
You can find the sum of two numbers by using the addition operator in Java. The result can be printed using System.out.println.
The following code illustrates the addition of two numbers in Java:-
Implementation
Java Code
Java Code
public class Main{ public static void main(String[] args) { int num1 = 5; int num2 = 10; int sum = num1 + num2; System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum); } }
You can also try this code with Online Java Compiler
In this section, we have discussed the Java program to add two integers where both integers are provided by the user and collected by the object of the scanner class.
Implementation
Java Code
Java Code
// Import the Scanner class import java.util.Scanner;
class Add_2_Numbers { public static void main(String[] args) {
// Creating a Scanner class object Scanner scan = new Scanner(System.in); System.out.println("Enter the first number : ");
// Reading user input int num1 = scan.nextInt();
System.out.println("Enter the second number : ");
// Reading user input int num2 = scan.nextInt();
// Calculate the sum with help of addition operator int sum = num1 + num2;
// Close the scanner scan.close();
// Print the sum System.out.println("The sum of " +num1+" and "+num2+" is: "+sum); } }
You can also try this code with Online Java Compiler
Method 3: Sum of Two Numbers Using Command Line Arguments in Java
Another method of taking input from users is command-line arguments. In this section, we have discussed the Java program to add two integers where both integers are provided by the user using arguments from the command line.
Implementation
Java Code
Java Code
public class Add_2_Numbers{ public static void main(String[] args){
// Reading user input int num1 = Integer.parseInt(args[0]); System.out.println("The First number is: "+num1);
// Reading user input int num2 = Integer.parseInt(args[1]); System.out.println("The Second number is: "+num2);
// Calculate the sum int sum = num1+num2;
// Print the sum System.out.println("The sum of " +num1+" and "+num2+" is: "+sum);
} }
You can also try this code with Online Java Compiler
Let us now discuss the Sum of Two Numbers using using methods:
By using a user-defined method
In the following code sample, the sum function expects two integers as input and returns their sum.
Implementation
Java Code
Java Code
public class Main { public static int sum(int x, int y) { return x + y; } public static void main(String[] args) { int num1 = 2; int num2 = 3; int result = sum(num1, num2); System.out.println("The sum of " + num1 + " and " + num2 + " is: " + result); } }
You can also try this code with Online Java Compiler
Next, let us see the built-in sum() method in java:
By using the sum() method
The sum() method of Java integer class returns the sum of the two given arguments.
The following code illustrates the same:-
Implementation
Java Code
Java Code
public class Main { public static void main(String[] args) { int num1 = 2; int num2 = 4; int result = Integer.sum(num1, num2); System.out.println("The sum of " + num1 + " and " + num2 + " is: " + result); } }
You can also try this code with Online Java Compiler
If the sum of integers in the given array exceeds the maximum value that an ‘int’ can store in Java, an integer overflow occurs. You will get to know more about it in the next section.
This condition occurs when you try to store a value larger than the limit of an integer. It usually occurs after performing some arithmetic operations involving many large integers. The following code sample illustrated the condition:-
Implementation
Java Code
Java Code
public class Main { public static void main(String[] args) { int maxValue = Integer.MAX_VALUE; // Maximum value of int
System.out.println("Max Value: " + maxValue);
int overflow = maxValue + 1; // Causes integer overflow
Adding one to the maximum value of the integer resulted in it becoming the lowest value that an integer can store. This is due to a wraparound behavior that is not present in C and C++.
The addition of two numbers in Java is very easy. Declare two variables and initialize those variables with some value. The next is to use another variable that will store the addition of the above-declared variables using the addition operator.
What is the sum of += in Java?
The += operator in Java is a shorthand assignment operator that is used for performing addition and assignment in a single step. The right-hand side value is added to the variable on the left-hand side and then assign the result to the left-hand side.
How to add two numbers in an array in Java?
To add two numbers in an array in Java, it is compulsory that the length of both the array must be the same. Create two arrays of the same length and initialize them with some values and later create a new array that will store the addition of two previous arrays.
How to add two string integer in Java?
Java uses the addition operator for both addition and concatenation. When you declare two variables, either two variables of string integers or one string and one integer variable, the addition is the concatenation of both variables.
Conclusion
In the article, we have extensively discussed the addition of two numbers in Java.
We hope that this blog has helped you enhance your knowledge regarding the java programming language. If you would like to learn more about such content and practice some quality questions that require you to excel your preparations a notch higher. In that case, you can visit our Guided Path in Coding Ninjas Studio.To be more confident in data structures and algorithms, try out our DS and Algo Course. Until then, All the best for your future endeavours, and Keep Coding.
Live masterclass
Using Netflix data to master Power BI : Ace Data Analytics interview
by Ashwin Goyal, Product @ HRS Group, Ex - Udaan, OYO
07 Nov, 2024
01:30 PM
Get hired as an Amazon SDE : Resume building tips
by Anubhav Sinha, SDE2 @ Amazon
05 Nov, 2024
01:30 PM
Using Netflix data to master Power BI : Ace Data Analytics interview
by Ashwin Goyal, Product @ HRS Group, Ex - Udaan, OYO