Methods & Arrays

Java Methods

Learn how to define and use methods in Java — parameters, return types, method overloading, and best practices.

What is a Method?

A method is a reusable block of code that performs a specific task. Methods help organize code, avoid repetition, and make programs easier to read and maintain.

java
// Method definition
public static int add(int a, int b) {
    return a + b;
}

// Method call
int result = add(5, 3);  // result = 8

Method Syntax

java
accessModifier returnType methodName(parameterList) {
    // method body
    return value;  // if returnType is not void
}
PartDescriptionExample
Access ModifierVisibility levelpublic, private, protected
Return TypeType of value returnedint, String, void
Method NameName to call the methodcalculateTotal
ParametersInput values(int a, int b)

Methods with Return Values

java
public static double calculateArea(double radius) {
    return Math.PI * radius * radius;
}

public static String greet(String name) {
    return "Hello, " + name + "!";
}

public static void main(String[] args) {
    double area = calculateArea(5.0);
    System.out.printf("Area: %.2f%n", area);       // Area: 78.54

    String message = greet("Arjun");
    System.out.println(message);                     // Hello, Arjun!
}

Void Methods

Methods that don't return a value use void:

java
public static void printLine(int length) {
    for (int i = 0; i < length; i++) {
        System.out.print("─");
    }
    System.out.println();
}

public static void main(String[] args) {
    printLine(30);
    System.out.println("Java Methods Tutorial");
    printLine(30);
}

Method Overloading

Multiple methods with the same name but different parameters:

java
public static int add(int a, int b) {
    return a + b;
}

public static double add(double a, double b) {
    return a + b;
}

public static int add(int a, int b, int c) {
    return a + b + c;
}

public static void main(String[] args) {
    System.out.println(add(5, 3));         // 8 (int version)
    System.out.println(add(2.5, 3.7));     // 6.2 (double version)
    System.out.println(add(1, 2, 3));      // 6 (three-param version)
}

Variable Arguments (Varargs)

Accept a variable number of arguments:

java
public static int sum(int... numbers) {
    int total = 0;
    for (int n : numbers) {
        total += n;
    }
    return total;
}

public static void main(String[] args) {
    System.out.println(sum(1, 2));           // 3
    System.out.println(sum(1, 2, 3, 4, 5));  // 15
}

Recursion

A method that calls itself:

java
public static int factorial(int n) {
    if (n <= 1) return 1;         // Base case
    return n * factorial(n - 1);   // Recursive call
}

public static void main(String[] args) {
    System.out.println(factorial(5));   // 120 (5×4×3×2×1)
}

Practical Example

java
public class MathUtils {
    public static boolean isPrime(int n) {
        if (n < 2) return false;
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0) return false;
        }
        return true;
    }

    public static void printPrimes(int start, int end) {
        System.out.printf("Primes between %d and %d: ", start, end);
        for (int i = start; i <= end; i++) {
            if (isPrime(i)) {
                System.out.print(i + " ");
            }
        }
        System.out.println();
    }

    public static void main(String[] args) {
        printPrimes(1, 50);
    }
}

Output:

Primes between 1 and 50: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

Summary

  • Methods are reusable blocks of code defined with a return type, name, and parameters
  • Use void for methods that don't return a value
  • Method overloading allows same-name methods with different parameter lists
  • Varargs (int... numbers) accept variable-length arguments
  • Recursion is when a method calls itself — always need a base case
  • Follow naming convention: camelCase for method names (e.g., calculateTotal)