While defining method, variables passed in the method are called parameters. While using those methods, values passed to those variables are called arguments.
In Java, the terms “argument” and “parameter” are often used interchangeably, but they have distinct meanings:
- Parameter:
- A parameter is a variable that is listed in the method declaration.
- It is a placeholder for the actual value (argument) that will be passed to the method when it is invoked.
- Parameters are part of the method’s signature and are defined in the method declaration.
- Parameters are used to specify what kind of data a method can accept.
Example:
javavoid exampleMethod(int x, String y) {
// x and y are parameters
// method implementation here
}
- Argument:
- An argument is the actual value that is passed to a method when it is invoked.
- It is the concrete value that is assigned to the corresponding parameter during a method call.
- Arguments are provided when calling a method and they must match the data type and order of the parameters defined in the method.
Example:
java// 10 and "Hello" are arguments
exampleMethod(10, "Hello");
In summary, parameters are variables in the method declaration, and arguments are the actual values passed to the method when it is called. The parameter acts as a placeholder that is later assigned the value of the corresponding argument during the method invocation.