Adding two numbers is usually the very first thing people learn when starting with Java programming. It may look simple, but this small step teaches you many important ideas such as variables, data types, and how Java runs code. Once you understand how addition works, you can easily move on to more complex programs.
In real life, adding numbers is everywhere. Java programs use addition when calculating prices in shopping apps, scores in games, totals in reports, or balances in banking systems. That is why learning how to add two numbers in Java is not just an academic exercise, but a real skill you will use again and again as you grow as a programmer.
Program 1: Adding two integer numbers
This program shows the simplest way to add two whole numbers in Java. It uses predefined integer values and prints the result.
public class Main {
public static void main(String[] args) {
int firstNumber = 10;
int secondNumber = 20;
int sum = firstNumber + secondNumber;
System.out.println("Sum: " + sum);
}
}In this program, two integer variables store the numbers we want to add. Java uses the plus symbol to perform addition. The result is stored in another variable and then displayed on the screen, making it easy for beginners to understand how values move through the program.
Program 2: Adding two decimal numbers using double
This program demonstrates how to add numbers that contain decimal points. It uses the double data type, which is common in Java for such values.
public class Main {
public static void main(String[] args) {
double price = 12.50;
double tax = 2.75;
double total = price + tax;
System.out.println("Total amount: " + total);
}
}Here, Java handles decimal numbers smoothly using the double type. This is useful in real-world applications like billing systems or measurements. Beginners should note that the logic is the same as integers, only the data type changes.
Program 3: Adding an integer and a decimal number
Sometimes you need to add different types of numbers. This program shows how Java handles adding an integer and a decimal together.
public class Main {
public static void main(String[] args) {
int items = 5;
double itemPrice = 9.99;
double totalCost = items + itemPrice;
System.out.println("Total cost: " + totalCost);
}
}Java automatically converts the integer into a decimal before performing the addition. This process is called type promotion. Beginners benefit from this because Java reduces errors and keeps calculations accurate.
Program 4: Adding large numbers using long
When working with very large numbers, the int type may not be enough. This program shows how to add large values using long.
public class Main {
public static void main(String[] args) {
long populationCityA = 1500000000L;
long populationCityB = 800000000L;
long totalPopulation = populationCityA + populationCityB;
System.out.println("Total population: " + totalPopulation);
}
}The long data type allows Java to store much bigger numbers safely. This is useful in systems dealing with large counts, such as population data or financial records. Beginners learn here that choosing the right data type matters.
Program 5: Adding two numbers entered by the user
This program allows the user to enter two numbers and see their sum. It introduces basic user input in Java.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter first number: ");
int firstNumber = input.nextInt();
System.out.print("Enter second number: ");
int secondNumber = input.nextInt();
int sum = firstNumber + secondNumber;
System.out.println("Sum: " + sum);
}
}The Scanner class is used to read input from the keyboard. The numbers entered by the user are stored in variables and then added together. This program helps beginners see how Java interacts with real users, not just fixed values.
Frequently Asked Questions (FAQ)
This section answers common beginner questions about adding numbers in Java.
Q1. Which symbol is used for addition in Java?
Java uses the plus sign to add two numbers.
Q2. Can Java add decimal numbers?
Yes, Java can add decimal numbers using float or double data types.
Q3. What happens when I add an int and a double?
Java automatically converts the int into a double before adding them.
Q4. Why do some numbers use L at the end?
The letter L tells Java that the number is a long value.
Q5. Is user input required to add numbers in Java?
No, user input is optional, but it makes programs more interactive and useful.
Conclusion
Adding two numbers in Java is simple, but it teaches many important programming concepts. You have seen how to add integers, decimals, mixed types, large numbers, and even values entered by the user. Each example builds confidence and shows how Java handles numbers in different situations.
To improve your skills, try changing the numbers, switching data types, or combining addition with other operations. With practice, these small programs will help you write more powerful and meaningful Java applications.




