Methods in Java

1. Introduction to Methods

1.1 What is a Method

A method is a block of code that performs a specific task and can be reused whenever needed.

1.2 Importance of Methods

  • Reduces code repetition
  • Improves readability
  • Makes debugging easier

1.3 Advantages

  • Code reusability
  • Modularity
  • Better organization

1.4 Real-World Analogy

A method is like a machine: you give input, it processes it, and gives output.


2. Method Declaration and Definition

2.1 Syntax

returnType methodName(parameters) {
// method body
}

2.2 Components

  • Access modifier (public, private)
  • Return type (int, void, etc.)
  • Method name
  • Parameters
  • Method body

2.3 Example

public static void greet() {
    System.out.println("Hello!");
}

2.4 Calling a Method

greet();

2.5 Static vs Non-Static

  • Static → called without object
  • Non-static → requires object

3. Method Parameters

3.1 What are Parameters

Parameters are inputs passed to methods.

3.2 Example

public static void add(int a, int b) {
    System.out.println(a + b);
}

3.3 Calling with Arguments

add(5, 3);

3.4 Pass by Value

Java always passes values, not references.

3.5 Multiple Parameters

You can pass multiple values:

multiply(int x, int y, int z);

4. Return Types

4.1 What is Return Type

Specifies what a method returns.

4.2 void Method

public static void show() {
    System.out.println("No return value");
}

4.3 Non-void Method

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

4.4 Returning Values

int result = sum(5, 3);

4.5 Multiple Values

Use arrays or objects.


5. Method Overloading

5.1 Definition

Method overloading means having multiple methods with the same name but different parameters.

5.2 Example

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

5.3 Rules

  • Must differ in parameters
  • Return type alone is not enough

5.4 Benefits

  • Improves readability
  • Reuse method names

6. Recursion

6.1 What is Recursion

A method calling itself is called recursion.

6.2 Base Case

Stops the recursion.

6.3 Example (Factorial)

public static int factorial(int n) {
    if (n == 1) {
        return 1;
    }
    return n * factorial(n - 1);
}

6.4 How it Works

  • Function calls itself
  • Keeps reducing problem
  • Stops at base case

6.5 Example (Fibonacci)

public static int fib(int n) {
    if (n <= 1) return n;
    return fib(n-1) + fib(n-2);
}

6.6 Advantages

  • Simple and clean code
  • Good for problems like trees

6.7 Disadvantages

  • Can be slower
  • Uses more memory

6.8 Recursion vs Loop

  • Recursion → function calls
  • Loop → iteration

Conclusion

Methods are one of the most powerful features in Java. They help break programs into smaller, reusable parts, making code easier to write, understand, and maintain. Concepts like parameters, return types, overloading, and recursion are essential for building efficient Java applications.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *