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
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.