← Back to Home

Method Parameters

Method parameters in Java are variables defined in a method signature that receive values (arguments) when the method is called. They allow methods to be flexible, reusable, and dynamic, instead of working with fixed values. This is a core interview topic and fundamental for understanding method calls, data passing, and program flow.

What Are Method Parameters?

  • Variables declared in the method definition
  • Receive values from the caller
  • Exist only during method execution
  • Used to pass data into methods
int add(int a, int b) {
    return a + b;
}
          

Here, a and b are parameters.

Parameters vs Arguments (Important Distinction)

add(10, 20);
          
  • Parameters → Variables in method definition (a, b)
  • Arguments → Actual values passed (10, 20)

Types of Method Parameters in Java

1. Value Parameters (Pass by Value)

Java supports pass-by-value only.

void change(int x) {
    x = 50;
}

int a = 10;
change(a);
System.out.println(a); // 10
          

Why: A copy of the value is passed.

2. Object Reference Parameters (Still Pass by Value)

void change(int[] arr) {
    arr[0] = 100;
}

int[] a = {10, 20};
change(a);
System.out.println(a[0]); // 100
          

Explanation:

  • Copy of reference is passed
  • Object itself can be modified

Single vs Multiple Parameters

Single Parameter

void print(int x) {
    System.out.println(x);
}
          

Multiple Parameters

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

Parameter Data Types

Parameters can be:

  • Primitive types
  • Arrays
  • Objects
  • Strings
  • User-defined classes
void display(String name, int age) { }
          

Method Parameters with Arrays

void printArray(int[] arr) {
    for (int value : arr) {
        System.out.println(value);
    }
}
          

Varargs (Variable-Length Arguments) — Preview

Allows passing zero or more arguments.

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

Final Parameters

void show(final int x) {
    // x cannot be modified
}
          

Use case: Prevent accidental modification.

Parameter Order Matters

void display(int id, String name) { }

display(101, "John"); // Correct
display("John", 101); // Compile-time error
          

Parameter Scope & Lifetime

  • Parameters are local to the method
  • Destroyed after method execution
  • Cannot be accessed outside the method

Method Parameter Examples

1. Single Parameter Method

class Demo {
    static void printNumber(int x) {
        System.out.println(x);
    }

    public static void main(String[] args) {
        printNumber(10);
    }
}
          

Explanation:

  • x is a method parameter
  • Value passed during call
  • Output: 10

2. Multiple Parameters Method

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

    public static void main(String[] args) {
        add(5, 7);
    }
}
          

Explanation:

  • Parameters are comma-separated
  • Order matters
  • Output: 12

3. Parameter Order Matters

class Demo {
    static void show(int a, String b) {
        System.out.println(a + " " + b);
    }

    public static void main(String[] args) {
        show(10, "Java");
        // show("Java", 10); // Compile-time error
    }
}
          

Explanation:

  • Parameter order must match method signature
  • Strongly typed

4. Passing Literal Values as Parameters

class Demo {
    static void greet(String name) {
        System.out.println("Hello " + name);
    }

    public static void main(String[] args) {
        greet("Selenium");
    }
}
          

Explanation:

  • Direct literal passed
  • Output: Hello Selenium

5. Passing Variables as Parameters

class Demo {
    static void square(int n) {
        System.out.println(n * n);
    }

    public static void main(String[] args) {
        int x = 4;
        square(x);
    }
}
          

Explanation:

  • Variable value copied
  • Output: 16

6. Parameter Reassignment (Primitive)

class Demo {
    static void modify(int x) {
        x = 100;
    }

    public static void main(String[] args) {
        int a = 10;
        modify(a);
        System.out.println(a);
    }
}
          

Explanation:

  • Java is call by value
  • Original unchanged
  • Output: 10

7. Parameter Modification (Object)

class Demo {
    static void modify(StringBuilder sb) {
        sb.append(" Java");
    }

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello");
        modify(sb);
        System.out.println(sb);
    }
}
          

Explanation:

  • Object content modified
  • Output: Hello Java

8. Reassigning Object Parameter

class Demo {
    static void modify(StringBuilder sb) {
        sb = new StringBuilder("New");
    }

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Old");
        modify(sb);
        System.out.println(sb);
    }
}
          

Explanation:

  • Reference reassignment is local
  • Output: Old

9. Passing Array as Parameter

class Demo {
    static void printArray(int[] arr) {
        for (int x : arr) {
            System.out.print(x + " ");
        }
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        printArray(nums);
    }
}
          

Explanation:

  • Arrays are objects
  • Output: 1 2 3

10. Modifying Array Parameter

class Demo {
    static void change(int[] arr) {
        arr[0] = 99;
    }

    public static void main(String[] args) {
        int[] a = {1, 2, 3};
        change(a);
        System.out.println(a[0]);
    }
}
          

Explanation:

  • Array content changes
  • Output: 99

11. Passing null as Parameter

class Demo {
    static void print(String s) {
        System.out.println(s);
    }

    public static void main(String[] args) {
        print(null);
    }
}
          

Explanation:

  • null is valid for reference types
  • Output: null

12. Null Parameter Causing Exception

class Demo {
    static void length(String s) {
        System.out.println(s.length());
    }

    public static void main(String[] args) {
        // length(null); // NullPointerException
    }
}
          

Explanation:

  • Calling method on null causes runtime exception

13. Method with Varargs Parameter

class Demo {
    static void sum(int... nums) {
        int total = 0;
        for (int n : nums) {
            total += n;
        }
        System.out.println(total);
    }

    public static void main(String[] args) {
        sum(1, 2, 3);
        sum(5, 10);
    }
}
          

Explanation:

  • Varargs treated as array
  • Output:
  • 6
  • 15

14. Varargs Must Be Last Parameter

class Demo {
    static void test(String name, int... nums) {
        System.out.println(name);
    }

    // static void test(int... nums, String name) ❌
}
          

Explanation:

  • Varargs must be last in method signature

15. Passing Expression as Parameter

class Demo {
    static void show(int x) {
        System.out.println(x);
    }

    public static void main(String[] args) {
        show(5 + 3);
    }
}
          

Explanation:

  • Expression evaluated before passing
  • Output: 8

16. Parameter Shadowing Variable Name

class Demo {
    static int x = 10;

    static void show(int x) {
        System.out.println(x);
    }

    public static void main(String[] args) {
        show(20);
    }
}
          

Explanation:

  • Parameter shadows class variable
  • Output: 20

17. Using this with Parameters

class Demo {
    int x;

    Demo(int x) {
        this.x = x;
    }

    void show() {
        System.out.println(x);
    }

    public static void main(String[] args) {
        Demo d = new Demo(10);
        d.show();
    }
}
          

Explanation:

  • this.x refers to instance variable
  • Output: 10

18. Parameter Passing in Method Overloading

class Demo {
    static void show(int x) {
        System.out.println("int");
    }

    static void show(Integer x) {
        System.out.println("Integer");
    }

    public static void main(String[] args) {
        show(10);
    }
}
          

Explanation:

  • Primitive preferred over wrapper
  • Output: int

19. Autoboxing in Parameters

class Demo {
    static void show(Integer x) {
        System.out.println(x);
    }

    public static void main(String[] args) {
        show(20);
    }
}
          

Explanation:

  • int → Integer (autoboxing)
  • Output: 20

20. Interview Summary – Method Parameters

class Demo {
    static void test(int x, int[] arr) {
        x = 50;
        arr[0] = 99;
    }

    public static void main(String[] args) {
        int a = 10;
        int[] b = {1, 2};

        test(a, b);

        System.out.println(a);
        System.out.println(b[0]);
    }
}
          

Explanation:

  • Primitive unchanged
  • Object modified
  • Output:
  • 10
  • 99

Common Beginner Mistakes

  • Confusing parameters with arguments
  • Assuming Java is pass-by-reference
  • Changing primitive parameters expecting caller value to change
  • Incorrect parameter order or type
  • Overloading confusion

Interview-Ready Answers

Short Answer

Method parameters are variables defined in a method that receive values when the method is called.

Detailed Answer

In Java, method parameters allow data to be passed into methods. Java uses pass-by-value, meaning a copy of the value or reference is passed. Primitive changes do not affect the caller, but object content can be modified through references.

Key Takeaway

Method parameters make methods dynamic and reusable. Understanding pass-by-value behavior, especially with objects, is crucial for writing correct and predictable Java programs.