A method becomes significantly more useful when it can work with different data. Instead of creating a separate method for every possible value, Java allows you to pass data into a method through parameters.
Think of parameters as input slots. The method defines what kind of information it expects, and the caller supplies the actual values when the method is invoked.
What Are Parameters?
A parameter is a variable declared inside a method's parentheses. It receives a value when the method is called.
static void greet(String name) {
System.out.println("Hello, " + name);
}
In this example, name is a parameter and String is its data type. The method can now work with different names without changing its implementation.
Why Do We Need Parameters?
Without parameters, a method would often be limited to fixed values. Parameters make methods reusable and flexible.
static void showStudent() {
System.out.println("Student: Rahul");
}
The method above is tied to one particular student. A parameter gives us a much better design:
static void showStudent(String name) {
System.out.println("Student: " + name);
}
Now the same method can display information for any student.
showStudent("Rahul");
showStudent("Anita");
showStudent("Priya");
This is one of the most important reasons methods use parameters: one method can handle many different inputs.
Parameter Syntax
A parameter generally follows this structure:
dataType parameterName
For example:
int age
double salary
String name
boolean active
Multiple parameters are separated by commas.
static void displayEmployee(String name, int age, double salary) {
System.out.println(name);
System.out.println(age);
System.out.println(salary);
}
Single Parameter
A method can have just one parameter.
static void printNumber(int number) {
System.out.println("Number: " + number);
}
The method can be called with different integer values.
printNumber(10);
printNumber(50);
printNumber(100);
Multiple Parameters
A method can accept multiple parameters, and each parameter can have its own data type.
static double calculateBill(double price, int quantity) {
return price * quantity;
}
Here, price is a double and quantity is an int.
double total = calculateBill(149.50, 3);
System.out.println(total);
The method combines the supplied price and quantity to calculate the total bill.
Parameter Types
Parameters can use primitive data types as well as reference types.
| Parameter Type | Example | Typical Use |
|---|---|---|
| int | int age | Whole numbers |
| double | double price | Decimal values |
| char | char grade | Single characters |
| boolean | boolean active | True or false values |
| String | String name | Text |
| Array | int[] marks | Multiple values of the same type |
| Object | Student student | Objects and custom data |
Parameters vs Arguments
Beginners frequently use the terms parameter and argument interchangeably. They are related, but they refer to different things.
static void greet(String name) {
System.out.println("Hello " + name);
}
greet("Rahul");
Here, name is the parameter because it appears in the method declaration. "Rahul" is the argument because it is the actual value supplied during the method call.
| Term | Where It Appears | Example |
|---|---|---|
| Parameter | Method declaration | String name |
| Argument | Method invocation | "Rahul" |
Remember: Parameters are declared by the method; arguments are supplied by the caller.
Parameter Order Matters
When a method has multiple parameters, arguments are matched according to their position.
static void display(String name, int age) {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
display("Rahul", 22);
The first argument is assigned to name, while the second argument is assigned to age.
The argument order must also be compatible with the declared parameter types.
Parameters with Return Values
Parameters and return values often work together. Parameters provide input, the method processes that input, and the return value provides the result.
static int calculateSquare(int number) {
return number * number;
}
int result = calculateSquare(8);
System.out.println(result);
The value 8 enters through the parameter, the method performs the calculation, and 64 is returned to the caller.
Using Multiple Data Types
Real applications often require methods to accept different kinds of information at the same time.
static void createProfile(String name, int age, double height, boolean active) {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Height: " + height);
System.out.println("Active: " + active);
}
createProfile("Anita", 24, 165.5, true);
Each argument is matched with its corresponding parameter according to position and compatible type.
Parameters Are Local to the Method
A parameter behaves like a local variable. It exists within the method's scope and cannot be directly accessed from outside that method.
static void showAge(int age) {
System.out.println(age);
}
The variable age belongs to this method. Once the method finishes execution, its parameter is no longer available as a local variable to the calling code.
Changing a Parameter Inside a Method
A parameter can be changed inside the method without changing the variable used as the argument in the calling code when a primitive value is passed.
static void changeValue(int number) {
number = 100;
System.out.println("Inside method: " + number);
}
public static void main(String[] args) {
int value = 50;
changeValue(value);
System.out.println("Outside method: " + value);
}
The parameter receives its own copy of the primitive value. The detailed behavior of this process is an important part of Java's pass-by-value model and will be covered separately.
Too Many Parameters
Parameters are powerful, but adding too many can make a method difficult to understand and use.
createUser(
"Rahul",
24,
"rahul@example.com",
"Bhubaneswar",
"India",
"Developer",
true,
45000.0
);
When a method requires a long list of related values, it may be better to group those values into an appropriate class or object. This keeps method calls easier to read and reduces the chance of passing values in the wrong order.
Professional Insight: A method with many parameters is not automatically wrong, but a long parameter list is worth examining. It may indicate that a related group of data deserves its own class.
Common Beginner Mistakes
- Forgetting to specify the data type of a parameter.
- Passing arguments in the wrong order.
- Passing a value that is incompatible with the parameter type.
- Confusing parameters with arguments.
- Creating methods with unnecessarily large parameter lists.
- Assuming that changing a primitive parameter automatically changes the original variable.
Best Practices
- Use descriptive parameter names such as price, quantity, and customerName.
- Choose parameter types that accurately represent the required data.
- Keep parameter lists reasonably small and understandable.
- Maintain a logical order for parameters.
- Avoid passing data that the method does not actually need.
Interview Insight
A common interview question is: “What is the difference between a parameter and an argument?” A concise answer is: “A parameter is a variable defined in a method declaration, while an argument is the actual value supplied when the method is invoked.”
Interviewers may also ask whether Java allows methods to accept different data types as parameters. The answer is yes. Each parameter can have its own valid Java type, including primitive types, arrays, strings, and objects.
Quick Revision
| Concept | Key Point |
|---|---|
| Parameter | A variable declared in a method to receive input. |
| Argument | The actual value supplied during a method call. |
| Single parameter | A method can accept one input value. |
| Multiple parameters | A method can accept several inputs separated by commas. |
| Parameter type | Defines the kind of value the method expects. |
| Parameter scope | A parameter is available within the method where it is declared. |
| Return value | A method can process parameter values and return a result. |
Parameters give Java methods the flexibility to work with changing input instead of fixed data. Once you understand the relationship between parameters and arguments, method calls become much easier to read, design, and debug—and the next natural step is understanding exactly what arguments are and how Java passes them to methods.
