How to add two numbers in Java?

In this tutorial we will learn How to add two numbers in Java?

How to add two numbers in Java?

In this tutorial we will learn How to add two numbers in Java?

Addition of two Number in Class

How to add two numbers in a Java program?

In Java you can easily add two number by using the plus (+) operator. The plus operator will add two numbers in Java it will return you int value as sum of two numbers. In real world programming you can make methods to add the numbers.

In this tutorial we are going to define two numbers and then use the plus operator for adding two numbers. The value will be returned as sum of the number.

So, we have to define two variables and then calculate the sum of it.

Description this program

In this example we are defining two in variables:

int x = 10;
int y= 20;

Then to calculate the sum we have used following code:

int sum= x+y;

Here is the code of this program:

package net.roseindia;

public class AddTwoNumbers {

	public static void main(String[] args) {
		int x = 10;
		int y= 20;
		int sum= x+y;
		System.out.println("Sum is: "+sum);

	}

}

If you run the program this will print:

Sum is: 30

So, in this program you learned how to sum(add) two numbers in Java.

Check more tutorials at: Java Conversion examples.