Methods are one of the most important building blocks in Java. They allow developers to break large programs into smaller, manageable, and reusable pieces of code. By using methods, a program becomes easier to understand, test, debug, and maintain. Methods also promote code reusability and follow the principle of modular programming.
A method is a block of code that performs a specific task and is executed only when it is called. In Java, a method must be declared with a name, return type, and parameters (optional). The method definition contains the actual implementation of the logic.
accessModifier returnType methodName(parameters) {
// method body
}
public, private, protected)int, String, void, etc.)public static void greet() {
System.out.println("Welcome to Java Programming");
}
The method greet() does not take any parameters and does not return any value. It simply prints a message.
Parameters are variables used to receive values from the calling method. Return types specify what type of value a method sends back after execution.
public static void add(int a, int b) {
System.out.println(a + b);
}
public static int multiply(int x, int y) {
return x * y;
}
add() accepts two integers and prints their sum.multiply() accepts two integers and returns their product.int, double, booleanString, arrays, objectsMethod overloading allows multiple methods with the same name but different parameter lists within the same class. It improves readability and flexibility.
public static int sum(int a, int b) {
return a + b;
}
public static double sum(double a, double b) {
return a + b;
}
public static int sum(int a, int b, int c) {
return a + b + c;
}
The sum() method behaves differently based on the number and type of parameters passed.
Varargs allow a method to accept zero or more arguments of the same type. This is useful when the number of arguments is not fixed.
returnType methodName(dataType... variableName)
public static int addNumbers(int... numbers) {
int sum = 0;
for (int num : numbers) {
sum += num;
}
return sum;
}
System.out.println(addNumbers(10, 20));
System.out.println(addNumbers(1, 2, 3, 4, 5));
Recursion is a programming technique in which a method calls itself to solve a problem. It is commonly used to solve problems that can be broken down into smaller sub-problems.
public static int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
The method keeps calling itself until the base condition (n == 0) is met.
public static int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
| Feature | Recursion | Iteration |
|---|---|---|
| Code Length | Short | Longer |
| Memory Usage | High (stack) | Low |
| Performance | Slower | Faster |
| Complexity | Easy to understand | Sometimes complex |
java
public class MethodsDemo {
// Simple method
public static void greet(String name) {
System.out.println("Hello, " + name);
}
// Method with return type
public static int add(int a, int b) {
return a + b;
}
// Method overloading
public static int multiply(int a, int b) {
return a * b;
}
public static double multiply(double a, double b) {
return a * b;
}
// Varargs
public static int sum(int... numbers) {
int total = 0;
for (int num : numbers) {
total += num;
}
return total;
}
// Recursion - Factorial
public static int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);
}
public static void main(String[] args) {
greet("Alice");
System.out.println("Sum: " + add(5, 3));
System.out.println("Product: " + multiply(4, 5));
System.out.println("Sum of multiple: " + sum(1, 2, 3, 4, 5));
System.out.println("Factorial of 5: " + factorial(5));
}
}