Java Program to Add Two Numbers Using Methods (With and Without Return Value)

When learning Java, understanding how to use methods is an important step toward writing clean and reusable code. Methods help us divide a program into smaller parts and avoid repeating logic.

In this tutorial, we will write a Java program to add two numbers using methods. We will cover two different approaches:

  1. Method with return value – the method calculates the sum and returns it to the caller.
  2. Method without return value – the method calculates the sum and directly prints it without returning.

By the end of this tutorial, you will clearly understand the difference between methods that return values and those that don’t.


Steps to Create the Program

  1. Create a new Java class named AddTwoNumbers.
  2. Define a method with return value:
    • Use public int AddWithReturn(int num1, int num2).
    • Inside the method, return the sum of the two numbers.
  3. Define a method without return value:
    • Use public void AddWithoutReturn(int num1, int num2).
    • Inside the method, calculate the sum and print it using System.out.println().
  4. Write the main method:
    • Inside main, create an object of the AddTwoNumbers class (since methods are non-static).
    • Initialize two integers, for example, a = 15 and b = 25.
  5. Call the method with return value:
    • Pass the two numbers to AddWithReturn.
    • Store the returned result in a variable and print it.
  6. Call the method without return value:
    • Pass the two numbers to AddWithoutReturn.
    • The result will be printed directly inside the method.
  7. Run the program and observe the output.

Java Program to Add Two Numbers Using Methods

  1. Method with return value – returns the sum of two integers.
  2. Method without return value – directly prints the sum without returning it.

Input and Output

InputMethodOutput
15, 25AddWithReturn()40
15, 25AddWithoutReturn()Sum = 40
package ebhor.methods;

public class AddTwoNumbers {

    // Method with return value
    public int AddWithReturn(int num1, int num2) {
        return num1 + num2;
    }

    // Method without return value
    public void AddWithoutReturn(int num1, int num2) {
        int sum = num1 + num2;
        System.out.println("Sum = " + sum);
    }

    public static void main(String[] args) {
        AddTwoNumbers obj = new AddTwoNumbers();

        int a = 15, b = 25;

        // Using method with return value
        int sum = obj.AddWithReturn(a, b);
        System.out.println("Addition = " + sum);

        // Using method without return value
        obj.AddWithoutReturn(a, b);
    }
}

Output

Addition = 40
Sum = 40

Java Program Explanation (Line by Line)

package ebhor.methods;
  1. Declares the package nameebhor.methods.
    • This organizes your classes into a folder (namespace).
    • Optional for small programs, but good practice for larger projects.
public class AddTwoNumbers {
  1. Defines a public class named AddTwoNumbers.
    • All your code (methods + main) will be inside this class.
public int AddWithReturn(int num1, int num2) {
    return num1 + num2;
}
  1. Defines a method with return value:
    • public → accessible from anywhere.
    • int → method returns an integer.
    • AddWithReturn(int num1, int num2) → takes two integers as input.
    • return num1 + num2; → calculates sum and returns it to caller.
public void AddWithoutReturn(int num1, int num2) {
    int sum = num1 + num2;
    System.out.println("Sum = " + sum);
}
  1. Defines a method without return value:
    • public void → does not return anything.
    • AddWithoutReturn(int num1, int num2) → takes two integers as input.
    • int sum = num1 + num2; → calculates sum.
    • System.out.println("Sum = " + sum); → directly prints result instead of returning.
public static void main(String[] args) {
  1. The main method:
    • Entry point of every Java program.
    • Execution starts from here.
AddTwoNumbers obj = new AddTwoNumbers();
  1. Creates an object of AddTwoNumbers class.
    • Needed because methods are non-static.
    • Using obj, we can call AddWithReturn and AddWithoutReturn.
int a = 15, b = 25;
  1. Declares two integers a and b.
    • These are the input numbers for addition.
int sum = obj.AddWithReturn(a, b);
System.out.println("Addition = " + sum);
  1. Calls AddWithReturn method:
    • Passes a=15 and b=25.
    • Method returns 40.
    • Stores result in variable sum.
    • Prints: Addition = 40.
obj.AddWithoutReturn(a, b);
  1. Calls AddWithoutReturn method:
    • Passes a=15 and b=25.
    • Method prints directly: Sum = 40.
}
  1. Closes the main method and the class.

Final Output

Addition = 40
Sum = 40