Add two number Java program

Java is a popular programming language that is widely used in software development. One of the basic programming tasks in Java is adding two numbers together. In this article, we will discuss how to write a Java program that adds two numbers together.

The first step in writing a Java program to add two numbers is to declare two variables that will hold the numbers. In Java, variables are declared using the following syntax:

data_type variable_name = value;

Here, the data_type specifies the type of data that the variable can hold, and the value is the initial value assigned to the variable. For example, to declare two variables to hold two integers, we would use the following code:

int num1 = 5;
int num2 = 10;

Once the variables have been declared and initialized, we can add them together using the + operator. Here is the complete code for a Java program that adds two numbers together:

public class AddTwoNumbers {
   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);
   }
}


Java is a popular programming language that is widely used in software development. One of the basic programming tasks in Java is adding two numbers together. In this article, we will discuss how to write a Java program that adds two numbers together.

The first step in writing a Java program to add two numbers is to declare two variables that will hold the numbers. In Java, variables are declared using the following syntax:

data_type variable_name = value;

Here, the data_type specifies the type of data that the variable can hold, and the value is the initial value assigned to the variable. For example, to declare two variables to hold two integers, we would use the following code:

int num1 = 5;
int num2 = 10;

Once the variables have been declared and initialized, we can add them together using the + operator. Here is the complete code for a Java program that adds two numbers together:

public class AddTwoNumbers {
   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);
   }
}

Let’s break down this code line by line.

  • The first line defines a public class named AddTwoNumbers.
  • The second line defines a public static method named main, which is the entry point for the program.
  • Inside the main method, we declare and initialize two integer variables num1 and num2 with the values 5 and 10, respectively.
  • We then declare and initialize an integer variable named sum that holds the sum of num1 and num2.
  • Finally, we use the System.out.println() method to output the sum to the console, along with a message that explains what the output represents.

When we run this program, the output will be:

The sum of 5 and 10 is 15

This program demonstrates how to add two numbers together in Java. It’s a simple but important concept that is used in many different programming tasks. By understanding the basics of Java syntax and data types, you can create more complex programs that perform a wide variety of tasks.