Method Parameters: Formal vs Actual Parameters
What you'll learn: You'll understand the difference between the parameters you declare in a method definition and the arguments you pass when calling that method.
The Two Sides of Parameters
When working with methods, you encounter parameters in two different contexts:
Formal parameters (also called "parameters") are the variables you declare in the method signature. They act as placeholders that define what data the method expects to receive.
Actual parameters (also called "arguments") are the real values or variables you pass into the method when you call it.
Think of it like a recipe: the formal parameters are the ingredient list ("2 cups flour, 1 egg"), while the actual parameters are the specific items you grab from your pantry when cooking.
Seeing the Distinction in Code
// Formal parameters: x and y
public static int add(int x, int y) {
return x + y;
}
public static void main(String[] args) {
int num1 = 5;
int num2 = 3;
// Actual parameters: num1 and 3
int sum = add(num1, 3);
}
In this example:
int xandint yare formal parameters — they exist in the method definitionnum1and3are actual parameters — they're the concrete values passed during the method call
The formal parameters define the contract (what types and how many). The actual parameters fulfill that contract by providing the actual data.
Key Takeaway: Formal parameters are the variable declarations in a method's signature; actual parameters are the specific values or variables you pass when invoking the method. The names don't have to match, but the types and order must align.