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:
- Method with return value – the method calculates the sum and returns it to the caller.
- 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
- Create a new Java class named
AddTwoNumbers. - Define a method with return value:
- Use
public int AddWithReturn(int num1, int num2). - Inside the method, return the sum of the two numbers.
- Use
- 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().
- Use
- Write the
mainmethod:- Inside
main, create an object of theAddTwoNumbersclass (since methods are non-static). - Initialize two integers, for example,
a = 15andb = 25.
- Inside
- Call the method with return value:
- Pass the two numbers to
AddWithReturn. - Store the returned result in a variable and print it.
- Pass the two numbers to
- Call the method without return value:
- Pass the two numbers to
AddWithoutReturn. - The result will be printed directly inside the method.
- Pass the two numbers to
- Run the program and observe the output.
Java Program to Add Two Numbers Using Methods
- Method with return value – returns the sum of two integers.
- Method without return value – directly prints the sum without returning it.
Input and Output
| Input | Method | Output |
|---|---|---|
| 15, 25 | AddWithReturn() | 40 |
| 15, 25 | AddWithoutReturn() | 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;
- Declares the package name
ebhor.methods.- This organizes your classes into a folder (namespace).
- Optional for small programs, but good practice for larger projects.
public class AddTwoNumbers {
- 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;
}
- 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);
}
- 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) {
- The main method:
- Entry point of every Java program.
- Execution starts from here.
AddTwoNumbers obj = new AddTwoNumbers();
- Creates an object of
AddTwoNumbersclass.- Needed because methods are non-static.
- Using
obj, we can callAddWithReturnandAddWithoutReturn.
int a = 15, b = 25;
- Declares two integers
aandb.- These are the input numbers for addition.
int sum = obj.AddWithReturn(a, b);
System.out.println("Addition = " + sum);
- Calls
AddWithReturnmethod:- Passes
a=15andb=25. - Method returns
40. - Stores result in variable
sum. - Prints:
Addition = 40.
- Passes
obj.AddWithoutReturn(a, b);
- Calls
AddWithoutReturnmethod:- Passes
a=15andb=25. - Method prints directly:
Sum = 40.
- Passes
}
- Closes the
mainmethod and the class.
Final Output
Addition = 40
Sum = 40