Difference between arguments and parameters in Java [duplicate]
Possible Duplicate:
What's the difference between an argument and a parameter?
I was going through some interview questions. I wasn't able to come up with a solid answer to this question:
Difference between arguments and parameters in Java?
How are they different?
Solution 1:
Generally a parameter is what appears in the definition of the method. An argument is the instance passed to the method during runtime.
You can see a description here: http://en.wikipedia.org/wiki/Parameter_(computer_programming)#Parameters_and_arguments
Solution 2:
The term parameter refers to any declaration within the parentheses following the method/function name in a method/function declaration or definition; the term argument refers to any expression within the parentheses of a method/function call. i.e.
- parameter used in function/method definition.
- arguments used in function/method call.
Please have a look at the below example for better understanding:
package com.stackoverflow.works;
public class ArithmeticOperations {
public static int add(int x, int y) { //x, y are parameters here
return x + y;
}
public static void main(String[] args) {
int x = 10;
int y = 20;
int sum = add(x, y); //x, y are arguments here
System.out.println("SUM IS: " +sum);
}
}
Thank you!
Solution 3:
There are different points of view. One is they are the same. But in practice, we need to differentiate formal parameters (declarations in the method's header) and actual parameters (values passed at the point of invocation). While phrases "formal parameter" and "actual parameter" are common, "formal argument" and "actual argument" are not used. This is because "argument" is used mainly to denote "actual parameter". As a result, some people insist that "parameter" can denote only "formal parameter".
Solution 4:
They are not. They're exactly the same.
However, some people say that parameters are placeholders in method signatures:
public void doMethod(String s, int i) {
..
}
String s and int i are sometimes said to be parameters. The arguments are the actual values/references:
myClassReference.doMethod("someString", 25);
"someString" and 25 are sometimes said to be the arguments.
Solution 5:
In java, there are two types of parameters, implicit parameters and explicit parameters. Explicit parameters are the arguments passed into a method. The implicit parameter of a method is the instance that the method is called from. Arguments are simply one of the two types of parameters.