Arguments in Java | Parameters, Example
Arguments in Java are the actual values that are passed to variables defined in the method header when the method is called from another method.
That is, whenever any particular method is called during the execution of the program, there are some values that are passed to call that method. These values are called arguments.
The passed argument values replace those parameters which have been used during method definition and the body of method is then executed with these values.
The type of argument values passed must be matched with the type specified for the corresponding parameter in the definition of method. Sometimes, arguments are also called actual parameters in Java.
Let’s take some different types of valid arguments examples to understand it better:
Example 1:
int x = add(5, 7);
int y = sum(35, 47);The values 5 and 7 are the arguments with which a method will be called by passing these values from another method.
Parameters in Java
In Java, a parameter is a variable name with type that is declared within the method signature. The list of parameters is enclosed in parentheses.
Each parameter consists of two parts: type name and variable name. A type name followed by a variable name defines the type of value that can be passed to a method when it is called. Parameters are also often called formal parameters in Java.
Parameters declared within the method signature are always local variables that receive values when the method is called by another method.
Example 2:
public int add(int a, int b)
{
return (a + b);
}The method add() has two parameters, named a and b with data type int. It adds the values passed in the parameters and returns the result to the method caller.
Example 3:
void sum(int x, int y)
The sum() method has two parameters: x and y. While passing the argument values to the parameters, the order of parameters and number of parameters is very important. These must be in the same order as their respective parameters declared in the method declaration.
Example 4:
public static void main(String[] args)
{
. . . . . . .
}In the main() method, args is a string array parameter.
Example 5:
void sub()
Here, the sub() method has no parameter. If a method has no parameters, then only an empty pair of parentheses is used.
Parameter Types
We can use any data type, such as primitive data types including int, float, double, char, short, byte, string, and object reference variables for a parameter of a method and constructor.
There is no standard limit to specify the number of parameters in the definition of a method. But you should limit the number of parameters almost 6 to 7. Any more will have a negative effect on the readability of your code.
Let’s understand arguments and parameters in Java with an example program and a related diagram.
Example 6:
package methodCallingExample;
public class Sum {
public static void main(String[] args)
{
// Creating an object of class Sum.
Sum obj = new Sum();
// Calling the sum() method by passing argument values to the method's parameters.
// The returned value is stored in a variable named x of type int.
int x = obj.sum(20, 10);
System.out.println(x);
}
// Method declaration with a return statement.
int sum (int a, int b)
{
int s = a + b;
return s;
}
}Output:
30Explanation with diagram:
In the preceding program and diagram, you can see that the method sum() has two parameters, a and b, both of which are of type int. So, both argument values must be of type int. Since we have not defined this method as static, we will call it by creating an object of the class.
When we will call sum() method from another method called main(), the parameters defined within the method signature are replaced with the values of passing arguments and the method is then executed with these values.
Inside the method sum(), we have declared a variable named s, which exists only within the body of the method. Each time this variable will be newly created when you will execute the method and it will be destroyed when the execution of method ends.
All the variables that you declare within the body of a method are considered as local variables. Variables declared within the body of a method are called local variables.
Difference between Arguments and Parameters in Java
The difference between parameter and argument is sometimes confusing in Java because many programmers often, incorrectly, use them interchangeably, but they both have different meanings. So, let’s understand the actual difference between them.
1. A parameter is a variable in the definition of a method, whereas an argument is an actual value of this variable that is passed to the method’s parameter.
2. A parameter is also called a formal parameter, whereas an argument is also called actual parameter.
3. During the time of call, each argument is always assigned to the parameter in the method definition, whereas parameters are local variables that are assigned by the value of arguments when the method is called.
Key Points to Remember:
1. Variables defines in the method header are called formal parameters or simply parameters.
2. Parameters in a method header are optional; that is, a method may or may not contain parameters.
3. If a method contains parameters, we need to pass values to parameters when a method is invoked. These values are called arguments or actual parameters.
4. A method header represents modifiers, return type value, method name, and parameters of the method.
5. The method name and parameter list together is called method signature.
6. Parameters defined in the method header are always local variables.






