# Java Program to Add the two Numbers

When we enter the coding world there are various mathematical operations that we learn to do through programming languages. We are expected to begin our coding journey with basic mathematical operations like adding, subtracting, multiplying and dividing. To evaluate the sum of two numbers, we use the '+' operator. However, Java also provides other approaches for adding two numbers. This article aims to explore the possible ways to add the two numbers in Java.

## Java Program to Add the Two Numbers

To add the two numbers in Java, we are going to use the following approaches −

• By taking input of operands from user

• By initializing the values at the time of declaration

• Using the sum() method

• Using command line arguments

Let's discuss them one by one.

## By taking input of operands from user

To take input from the keyboard, we need to create an instance of Scanner class which provides various built-in methods for user input. For instance, we can use the 'nextInt()' method if we need to enter an integer value.

### Syntax

Scanner nameOfinstance = new Scanner(System.in);


### Example

In the following example, we will accept two integer type operands using the Scanner class to perform the addition operation between them.

import java.util.Scanner;
public static void main(String[] args) {
int input_1, input_2, my_sum;
// Scanner to read input from user
Scanner my_scanner = new Scanner(System.in);
System.out.println("A reader object has been defined ");
System.out.println("Enter the first number: ");
// to take first operand
input_1 = my_scanner.nextInt();
System.out.println("Enter the second number: ");
// to take second operand
input_2 = my_scanner.nextInt();
my_scanner.close();
System.out.println("The scanner object has been closed");
// calculating and storing the result
my_sum = input_1 + input_2;
System.out.println("Sum of the two numbers is: ");
// to print the result
System.out.println(my_sum);
}
}


### Output

A reader object has been defined
Enter the first number:
55
Enter the second number:
44
The scanner object has been closed
Sum of the two numbers is:
99


## By initializing the values at the time of declaration

It is the simplest way of adding two numbers. We simply need to declare two operands and initialize them with the value of our choice. Also, we require a third variable to store the result of addition.

### Example

The following example illustrates the practical implementation of what we have discussed above.

public class NumberAddition {
public static void main(String[] args) {
// declaring and initializing operands
int value_1, value_2, my_sum;
value_1 = 10;
value_2 = 15;
System.out.println("The two operands are: " + value_1 + " and " + value_2);
my_sum = value_1 + value_2;
System.out.println("Sum of the given two numbers is : ");
// printing the result
System.out.println(my_sum);
}
}


### Output

The two operands are: 10 and 15
Sum of the given two numbers is :
25


## Using the sum() method

The sum() method is a static method of the Integer class that accepts two integer operands as an argument and returns the sum of those two operands. Note that it is a static method hence, we need to call it using the Integer class.

### Example

The following example demonstrates how to add two numbers using the sum() method.

public class NumberAddition {
public static void main(String[] args) {
// declaring and initializing operands
int value_1, value_2, my_sum;
value_1 = 10;
value_2 = 15;
System.out.println("The two operands are: " + value_1 + " and " + value_2);
my_sum = Integer.sum(value_1, value_2);
System.out.println("Sum of the given two numbers is : ");
// printing the result
System.out.println(my_sum);
}
}


### Output

The two operands are: 10 and 15
Sum of the given two numbers is :
25


## Using command line arguments

String[] args is a parameter of the Java main() method that accepts String type arguments. It allows us to pass arguments through the terminal and stores those arguments in an array of strings. We can say that String[] args is a command line argument.

### Example

In this example, we will take two operands of type double using command line and evaluate their sum.

public class Addition {
public static void main(String[] args) {
// to take input from terminal
double operand1 = Double.parseDouble(args[0]);
double operand2 = Double.parseDouble(args[1]);
double my_sum = operand1 + operand2;
// to print the result
System.out.println("Sum of the two numbers: " + my_sum);
}
}


### Output

PS D:\Java Programs> javac Addition.java
PS D:\Java Programs> java Addition 9.23 9.32
Sum of the two numbers : 18.55


## Conclusion

In this article, we have learned different ways of adding two numbers. The most simple and frequent way is to use '+' operator. There is a built-in method named sum() that is also used for adding two numbers. But, we can't add multiple values using this built-in method like we do with '+' operator.

Updated on: 10-Aug-2023

8K+ Views