This article will provide an in depth understanding of the below-listed concepts:
- What are parameters in java
- Parameters vs Arguments in Java
- Examples
- Conclusion
So, let’s get started!
What are parameters in java
The parameters in java are variables that can be passed to the methods or constructors. Within these methods/constructors these parameters behave as variables.
Parameters vs Arguments in Java
Let’s consider a simple java program to understand the difference between the parameters and arguments.
How to show a user-defined method that accepts a parameter
The below snippet shows a user-defined method that accepts a parameter:
In the above snippet, showAge() is the user-defined method that takes an integer type parameter. The static keyword shows that the method “showAge()” can be approached directly using the class name. Void keyword shows that the showAge() method doesn’t return anything.
The parameters will be passed to a method at the time of method calling as shown in the below given snippet:
The actual parameters(values) passed to the method at the time of method calling are known as the arguments.
Now let’s conclude this example to figure out what’s the key difference between arguments and parameters in java.
From the example given above, we come to know that the actual parameters/values that are passed to a method at the time of method calling are known as arguments (in this example 32 is an argument) while the parameters are the variables defined in the method’s header (in this example int age is a parameter).
How to pass a single parameter to a method in java
In this example, the user-defined method “printName()” utilizes only one parameter of integer data type. From the main method we passed two arguments to the printName() method. Following will be the resultant output for the above given java program:
This is how we pass a parameter to a method.
How to pass multiple parameters to a method in java
The following snippet shows how to use comma-separated syntax to pass more than one argument to a method:
public class ParametersExample {
static void printName(String userName, int userAge) {
System.out.println("Employee Name: " + userName);
System.out.println("Employee Age: " + userAge);
}
public static void main(String[] args) {
printName("Ambrose", 34);
printName("David", 24);
}
}
The output verifies the working of the above given java program.
How to pass parameters to a constructor in java
In this example, we have a constructor that accepts two parameters of integer data type. Afterward, it calculates the product of those numbers:
We passed the arguments to the constructor from the main method. The constructor accepted the parameters and performed the desired functionality on them.
Conclusion
The parameters in java are variables that can be passed to the methods or constructors. Within these methods or constructors these parameters act as variables. The actual parameters/values passed to a method at the time of method calling are known as arguments while the parameters are the variables defined in the method’s header. This post explained various aspects of parameters in java with the help of some suitable examples.