Arguments in Java


An argument in Java is an actual value that is passed to a method when the method is called. Whenever any particular method is called during the execution of the program, there are some values that are passed to call that particular method. These values are called arguments.

The passed argument values replace those parameters which have been used during method definition and the 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, an argument is also called actual parameter.
For example:
       add(5,7);
       sum(35, 47);
The values 5 and 7 are the arguments with which a method will be called. 

Java Parameter


In Java, a parameter is a variable name with type that is declared within method signature. The list of parameters is enclosed in parenthesis. Each parameter consists of two parts, a 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. It is also often called formal parameter. Parameters declared in method signature are always local variables that receive values when the method is called.
For example:
 1.  public int add(int a, int b){
      return (a+b);
   } 
The method add has two parameters, named a and b with data type integer. It adds the values passed in the parameters and returns the result to method caller.

 2.  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 are very important. These must be in the same order as their respective parameters declared in the method declaration.

3. public static void main(String[ ] args ) {
        . . . . . . .
 }
In the main() method, args is a String array parameter.

4. 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, 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 and any more will have a negative effect on the readability of your code.


Let's understand arguments and parameters with an example program and related diagram.
Program source code 1:
    package methodCallingExample; public class Sum { public static void main(String[] args) { Sum obj=new Sum(); int x=obj.sum(20, 10); System.out.println(x); } int sum (int a, int b){ int s=a+b; return s; } }
    Output: 30
Explanation with Diagram:
Arguments and parameters in Java
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 always be of type int. Since this method has not been defined as static, we can call it by creating an object of 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.

The method sum() declares a variable s, which exists only within the body of the method. Each time this variable will be newly created when you will execute method and it will be destroyed when the execution of method ends. All the variables that you declare within body of a method, are considered as local variables. Variables declared within body of a method are called local variables.

Difference between Argument and Parameter


The difference between a parameter and an argument is sometimes confusing in Java because many programmers often, incorrectly, use them interchangeably but they both have different meaning. So, let's see 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 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 which are assigned by value of the arguments when method is called.

Final words
Hope that this tutorial has covered almost all the important points related to arguments and parameters in Java with example program and related diagram. I hope that you will have understood the difference between arguments and parameters in Java and enjoyed it.