Introduction to Method Parameters
In Java, method parameters are the values you pass into a method so that it can perform a specific task using those inputs.
They make methods dynamic, flexible, and reusable, allowing the same block of code to work with different data each time it’s called.
Method Parameters as temporary data carriers; they receive values from the method call and use those values inside the method to perform operations or calculations.
Syntax for Parameters
Here is the syntax to define and use parameters in a method:
returnType methodName(dataType parameterName) {
// Method body
}
Why Are Method Parameters Important?
Method parameters play a key role in writing clean, reusable, and modular code. Here are the main reasons they are so important:
1) Reusability: With parameters, the same method can handle different inputs every time. You don’t have to write multiple versions of the same logic.
Example:
public int multiply(int a, int b) {
return a * b;
}
You can call this method with any pair of numbers:
multiply(5, 3); // 15
multiply(10, 4); // 40
2) Modularity: Parameters help break a program into smaller, manageable parts. Each method focuses on a specific task and works independently by using the values passed to it.
3) Flexibility: Parameters make methods more versatile. You can pass user input, calculated values, or data from other methods, giving full control over how the method behaves.
For example:
public void printMessage(String message, int times) {
for (int i = 1; i <= times; i++) {
System.out.println(message);
}
}
Now you can print any message any number of times:
printMessage("Java is fun!", 3);
Output:
Java is fun!
Java is fun!
Java is fun!
How Parameters Works in Methods?
When we call a method and pass a value to it:
- The value is copied into the method’s parameter.
- The method then uses that parameter inside its logic.
- Once the method finishes, the parameter’s memory is released.
For example:
public void displaySquare(int number) {
System.out.println("Square: " + (number * number));
}
displaySquare(4); // Output: Square: 16
- In this code, the value
4is passed to the parameter number. Inside the method, number temporarily holds the value 4 for the calculation.
Types of Method Parameters
Java supports two main types of method parameters:
1) Formal Parameters:
It is defined in the method declaration. They act as placeholders for the values that will be passed.
public void greet(String name) { ... } // 'name' is a formal parameter
2) Actual Parameters (Arguments):
These are the real values you pass when calling the method.
greet("Aarav"); // "Aarav" is an actual parameter
Examples of Methods with Parameters
Example 1: Single Parameter
public class GreetingExample {
// Method with one parameter
public void greet(String personName) {
System.out.println("Welcome, " + personName + "! Hope you’re doing great.");
}
public static void main(String[] args) {
GreetingExample obj = new GreetingExample();
obj.greet("Aarav"); // Passing "Aarav" as parameter
obj.greet("Mira"); // Passing "Mira" as parameter
}
}
Usage:
Welcome, Aarav! Hope you’re doing great.
Welcome, Mira! Hope you’re doing great.
- Here, greet(String personName) is a method that takes one String parameter.
When you call it with “Aarav”, it replaces personName with “Aarav” and prints the message. - So, one method can handle different names without rewriting the code.
Example 2: Multiple Parameters
public class CalculatorExample {
// Method with two parameters
public int addNumbers(int num1, int num2) {
int total = num1 + num2;
return total;
}
public static void main(String[] args) {
CalculatorExample calc = new CalculatorExample();
int result = calc.addNumbers(12, 8); // Passing two numbers
System.out.println("Sum of numbers: " + result);
}
}
Usage:
Sum of numbers: 20
- In this code, addNumbers(int num1, int num2) accepts two integer parameters. When you pass 12 and 8, the method adds them and returns the result.
Pass-by-Value in Java
In Java, every time you pass a variable to a method, Java creates a copy of that variable’s value, not the original one. This concept is called Pass-by-Value.
It means the method works with duplicate values, so any changes made inside the method do not affect the original variable outside the method.
Example: Understanding Pass-by-Value
public class PassByValueExample {
// Method trying to change the value
public void changeNumber(int num) {
num = 50; // Changes only the local copy
System.out.println("Inside method: " + num);
}
public static void main(String[] args) {
int original = 10;
PassByValueExample obj = new PassByValueExample();
obj.changeNumber(original); // Passing the variable to method
System.out.println("Outside method: " + original);
}
}
Output:
Inside method: 50
Outside method: 10
- When you call changeNumber(original);, Java copies the value of original (which is 10) and sends it to the method.
- Inside the method, the parameter num receives its own copy of the value (10).
- Changing num to 50 affects only that copy, not the actual variable original.
What Are Parameter Types in Java?
Java classifies parameters into three main types:
- Primitive Data Type Parameters
- Reference Data Type Parameters
- Variable Arguments (Varargs)
1. Primitive Data Types
These are the basic data types in Java, like int, double, char, Boolean, etc. When you pass them to a method, Java sends only a copy of their value, not the actual variable.
So, if you change the value inside the method, it won’t affect the original variable. For example:
public class PrimitiveParameterExample {
// Method that prints the square of a number
public void printSquare(int number) {
System.out.println("Square of " + number + " = " + (number * number));
}
public static void main(String[] args) {
PrimitiveParameterExample obj = new PrimitiveParameterExample();
obj.printSquare(6); // Output: Square of 6 = 36
}
}
- Here, the method printSquare() accepts an integer number as input and prints its square.
- We can pass any integer value, and the method will work for all.
2. Reference Data Type Parameters
Reference data types include objects, arrays, and classes. When you pass them to a method, Java actually sends a copy of the reference (memory address), not the object itself.
So, if the method changes the data inside the object, the changes will reflect outside too.
For example:
public class ReferenceParameterExample {
// Method that prints all elements of an array
public void printArray(int[] arr) {
System.out.print("Array elements: ");
for (int element : arr) {
System.out.print(element + " ");
}
System.out.println();
}
public static void main(String[] args) {
int[] numbers = {2, 4, 6, 8};
ReferenceParameterExample obj = new ReferenceParameterExample();
obj.printArray(numbers); // Output: Array elements: 2 4 6 8
}
}
- Here, the method printArray() takes an integer array as a parameter and prints each element.
- We can pass any array, and the method will handle it dynamically.
3. Variable Arguments (Varargs)
Sometimes, you don’t know how many arguments you’ll need to pass. You want a method that can print 2 numbers today and 10 tomorrow, without changing the method definition. That’s where Varargs (…) come in.
public class VarargsExample {
// Method that accepts a variable number of integer arguments
public void printNumbers(int... numbers) {
System.out.print("Numbers: ");
for (int number : numbers) {
System.out.print(number + " ");
}
System.out.println();
}
public static void main(String[] args) {
VarargsExample obj = new VarargsExample();
obj.printNumbers(1, 2, 3); // Output: Numbers: 1 2 3
obj.printNumbers(10, 20, 30, 40); // Output: Numbers: 10 20 30 40
}
}
- The method printNumbers() can take any number of integer inputs because of the int… numbers syntax.
- Inside the method, these arguments behave just like an array, allowing you to loop through them easily.
Returning Values Based on Parameters
In Java, a method can use the values passed through parameters to calculate or create a new result, and then return that result to the caller.
This feature makes methods powerful and reusable because the same method can produce different outputs depending on the inputs provided.
When a method has a return type (like int, String, double, etc.), it must use the return keyword to send a result back.
Example:
public class ReturnValueExample {
// Method that combines first and last name
public String fullName(String firstName, String lastName) {
// Combine both names with a space in between
return firstName + " " + lastName;
}
public static void main(String[] args) {
ReturnValueExample obj = new ReturnValueExample();
// Call the method with two string parameters
String name = obj.fullName("John", "Doe");
System.out.println("Full Name: " + name);
}
}
Output:
Full Name: John Doe
Exercise: Student Grade Calculator (Using Method Parameters)
Write a Java program that calculates and displays a student’s final grade based on their marks in three subjects, English, Math, and Science.
You must create a method that:
- Accepts the three marks as parameters.
- Calculates the average score.
- Returns a grade based on the following conditions:
| Average Marks | Grade |
|---|---|
| 90 – 100 | A+ |
| 80 – 89 | A |
| 70 – 79 | B |
| 60 – 69 | C |
| Below 60 | D |
The main method should then display both the average marks and the final grade.
Sample Output:
If the student scored:
- English = 85
- Math = 78
- Science = 92
Output:
Average Marks: 85.0
Final Grade: A
Example Solution For Reference
public class StudentGradeCalculator {
// Method to calculate grade based on marks
public String calculateGrade(int english, int math, int science) {
double average = (english + math + science) / 3.0;
String grade;
if (average >= 90) {
grade = "A+";
} else if (average >= 80) {
grade = "A";
} else if (average >= 70) {
grade = "B";
} else if (average >= 60) {
grade = "C";
} else {
grade = "D";
}
System.out.println("Average Marks: " + average);
return grade;
}
public static void main(String[] args) {
StudentGradeCalculator obj = new StudentGradeCalculator();
// Call method with parameters
String result = obj.calculateGrade(85, 78, 92);
// Display grade
System.out.println("Final Grade: " + result);
}
}
- Write this above code by your logic and understand the logic about parameters.
Also Learn Important Topics of Java
- What are the operators in Java Programming?
- What are Data Types in Java?
- What are the Variables in Java?
- Learn Java syntax.
- What is Java if else statements?

M.Sc. (Information Technology). I explain AI, AGI, Programming and future technologies in simple language. Founder of BoxOfLearn.com.