Java Method Parameters
Java method parameters are variables listed inside a method’s parentheses. They let a method receive values from the code that calls it.
Parameters make methods more flexible because the same method can work with different data each time it runs.
Parameters And Arguments
A parameter is the variable in the method definition. An argument is the actual value passed when the method is called.
public class Main {
static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
public static void main(String[] args) {
greet("Mia");
greet("Noah");
}
}
Output:
Hello, Mia!
Hello, Noah!
In static void greet(String name), name is a parameter. In greet("Mia"), "Mia" is an argument. When the method runs, name stores the value that was passed in.
Parameter Types
Every Java parameter needs a type and a name. The type tells Java what kind of value the method can accept, such as String, int, double, or boolean.
The argument must be compatible with the parameter type. For example, a method with an int parameter needs a whole number argument, not a sentence of text.
Multiple Parameters
A method can have more than one parameter. Separate parameters with commas, and pass arguments in the same order when calling the method.
public class Main {
static void printItem(String name, int quantity, double price) {
double total = quantity * price;
System.out.println(quantity + " " + name + " cost $" + total);
}
public static void main(String[] args) {
printItem("notebooks", 3, 2.5);
printItem("pens", 5, 1.2);
}
}
Output:
3 notebooks cost $7.5
5 pens cost $6.0
This method has three parameters: name, quantity, and price. The call printItem("notebooks", 3, 2.5) matches them in order: text first, then a whole number, then a decimal number.
Using Parameters In Calculations
Parameters behave like local variables inside the method. You can print them, compare them, or use them in calculations.
public class Main {
static int rectangleArea(int width, int height) {
return width * height;
}
public static void main(String[] args) {
int smallArea = rectangleArea(4, 3);
int largeArea = rectangleArea(10, 6);
System.out.println(smallArea);
System.out.println(largeArea);
}
}
Output:
12
60
The method uses width and height to calculate an area. Because it returns an int, the result can be stored in an int variable or printed directly.
Changing A Parameter
For simple values such as int, Java passes a copy of the value into the method. Changing the parameter inside the method does not change the original variable in main.
public class Main {
static void addBonus(int score) {
score = score + 10;
System.out.println("Inside method: " + score);
}
public static void main(String[] args) {
int playerScore = 50;
addBonus(playerScore);
System.out.println("After method: " + playerScore);
}
}
Output:
Inside method: 60
After method: 50
The method changes its own score parameter, but playerScore stays 50. To keep a changed value, return it from the method and store the result.
Common Mistakes
- Passing arguments in the wrong order, such as putting a price where a quantity is expected.
- Passing the wrong type of value for a parameter.
- Forgetting commas between parameters in the method definition or arguments in the method call.
- Expecting a method to change an
int,double, orbooleanvariable frommainwithout returning a new value.
Takeaway: parameters let Java methods receive values, and each call supplies arguments that must match the method’s parameter list.
