Java Program To Add Two Number

Adding two numbers is often the very first "real" logic a developer writes in Java. While it seems simple, this exercise introduces you to several fundamental concepts: Variables, Data Types, User Input, and Basic Arithmetic. In a real-world application, this logic is the foundation for everything from calculating the total price in a shopping cart to determining a player's score in a mobile game.

Here's a robust Java program to add two numbers entered by the user:

import java.util.Scanner;

public class AddTwoNumbers {
    public static void main(String[] args) {
        // Create a Scanner object to read input from the standard input stream (keyboard)
        Scanner scanner = new Scanner(System.in);

        System.out.println("--- Basic Addition Tool ---");

        // Prompt the user to enter the first number
        System.out.print("Enter the first number: ");
        double num1 = scanner.nextDouble();

        // Prompt the user to enter the second number
        System.out.print("Enter the second number: ");
        double num2 = scanner.nextDouble();

        // Perform the addition logic
        double sum = num1 + num2;

        // Display the result with a clear message
        System.out.println("Calculation Result: " + num1 + " + " + num2 + " = " + sum);

        // Close the Scanner to free up system resources
        scanner.close();
    }
}
Developer Tip: In professional environments, we often use double instead of int for addition scripts because it allows users to enter decimal values (like 10.5), making the program much more flexible.

How the Code Works

Let's break down exactly what happens when you run this program:

  • The Scanner Class: We import java.util.Scanner at the top. This is a built-in Java utility that allows our program to "listen" to what the user types on their keyboard.
  • System.in: When we initialize the Scanner with System.in, we are telling Java to read from the standard input stream.
  • Variables (num1, num2): These act as containers in your computer's memory. By declaring them as double, we tell Java to set aside enough space for large numbers with decimal points.
  • Input Methods: The nextDouble() method pauses the program and waits for the user to type a number and press Enter.
  • Resource Management: We use scanner.close() at the end. In Java, it is good practice to close "streams" once you are finished with them to prevent memory leaks.
Common Mistake: Beginners often forget that the Scanner class is not available by default. If you don't include the import java.util.Scanner; line at the very top of your file, your code will fail to compile.

Real-World Examples

While adding two numbers is a basic console exercise, this logic is applied everywhere in software development:

  • E-commerce: Adding the price of a product and the shipping fee to get the total order cost.
  • Finance: Adding a monthly interest amount to a bank account balance.
  • Gaming: Adding "Experience Points" (XP) to a player's current level progress.
Best Practice: Always provide clear prompts using System.out.print. It lets the user know exactly what kind of input the program is waiting for, improving the "User Experience" (UX) of your console tool.
Watch Out: If a user types letters (like "hello") instead of a number, the program will crash with an InputMismatchException. In advanced Java development, we use "Try-Catch" blocks to handle these types of user errors gracefully.
Developer Tip: If you only need to work with whole numbers (integers), you can swap double for int and use scanner.nextInt() instead. This uses slightly less memory!