In this blog, you will learn how to add two numbers in java. The resultant sum is displayed on the screen. Adding two numbers often seems to be too easy. We will discuss how to find the sum or addition of two numbers in Java. We have also discussed different ways through which users can provide input, and much more.

## What is addition of two numbers in Java?

The addition of two numbers in Java programming language means, given two integer numbers, we have to add those two numbers and have to print the sum of those two numbers. Addition of two numbers in Java is a simple task, lets see the algorithm for the addition of two numbers in Java programming language.

## Algorithm for addition of two numbers in Java:

- Declare two variables.
- Initialize the variables with some values.
- Declare another variable to store the sum of the numbers.
- Apply the mathematical plus(+) operator between two values and store the result.

### Method 1: Standard program for the addition of two numbers in java.

class SumOfNumbers { public static void main(String args[]) { int A = 25, B = 25, sum; sum = A + B; System.out.println("The sum of two numbers is: "+sum); } }

```
Output: The sum of two numbers is: 50
```

This is a straightforward approach to finding the sum of two numbers in Java. Lets have a look at how to find the sum of two numbers in java using the method.

There are two ways to find the addition of two numbers in java using the method

- By using the user-defined method
- By using the sum() method

### Method 2: Sum of two numbers in Java by using the user-defined method:

Java allows the user to read the input. We can take two numbers as input from the user and pass them to the user-defined method sum().

Below is the implementation of the Java program to add two numbers.

import java.util.Scanner; class SumOfNumbers2 { public static void main(String args[]) { int x, y, sum; Scanner sc = new Scanner(System.in); x = sc.nextInt(); y = sc.nextInt(); sum = sum(x, y); System.out.println("The sum of numbers " + x + " and " + y + " is: " +sum ); } public static int sum(int a, int b) { int sum = a + b; return sum; } }

`Output: The sum of numbers 10 and 20 is: 30`

### Method 3: Addition of two numbers in Java by using Integer.sum() method:

The integer class provides the sum() method. sum() method is a static method that adds two numbers as per operator.

`Syntax: public static int sum(int a, int b);`

class SumOfNumbers { public static void main(String args[]) { int x = 10, y = 20, a = 5, b = 5, sum, s; sum=Integer.sum(x, y); System.out.println("The sum of x and y is: " +sum); s=Integer.sum(a, b); System.out.println("The sum of x and y is: " +s); } }

```
Output:
The sum of x and y is: 30
The sum of x and y is: 10
```

### Method 4: Addition of two numbers in Java by using command line arguments

Another method to taking input from user is command line arguments. We will discuss the java program to add two numbers where both the numbers are provided by the user using arguments from the command line.

class SumOfNumbers4 { public static void main(String args[]) { int x = Integer.parseInt(args[0]); //first arguments int y = Integer.parseInt(args[1]); //second arguments int sum = x + y; System.out.println("The sum of x and y is: " +sum); } }

```
Output:
The sum of x and y is: 100
```

**Explanation:**

Compile the above program using the command javac SumofNumbers.java. After that, run the program by using command java SumofNumbers 50 50.

Where 50 and 50 are command line arguments.

**Conclusion:**

In this article, we have studied how to find the addition of two numbers in Java with different methods. Along with that we have also learned how to take input from user. Addition of two numbers in Java is a simple program, but have many different approaches for the addition of two numbers in Java. You have to figure out more approaches for the addition of two numbers in Java. We hope this blog has helped you to enhance your knowledge regarding the java programming language.

**Other Java Programs**

Java Program to Check Prime Number

Java Program to Check Whether a Number is a Palindrome or Not

Java Program to Find the Factorial of a Number

Java Program to Reverse a Number

Java Program to search an element in a Linked List

Program to convert ArrayList to LinkedList in Java

Java Program to Reverse a linked list

Java Program to search an element in a Linked List