Arguments in Java | Parameter in Java

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 particular 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, an argument is also called actual parameter.

Let’s take some different types of arguments examples to understand it better:

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.

Parameter 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 parenthesis.

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. It is also often called formal parameter.

Parameters declared in the method signature are always local variables that receive values when the method is called from another method.

For example:

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.

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 is 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 )

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, 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 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); 
int sum (int a, int b)
    int s = a + b; 
    return s; 

Explanation with diagram:

arguments 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 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.

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 the body of a method, are considered as local variables. Variables declared within the body of a method are called local variables.

Difference between Argument and Parameter 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 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 that are assigned by value of the arguments when method is called.

Hope that this tutorial has covered all the important points related to arguments and parameters in Java with an example program and related diagram.

I hope that you will have understood the difference between arguments and parameters in Java. Now, keep in mind the following key points.

Are you looking for Java assignment help online? Feel free visit this site to get your Java homework done by experts.

Key Points:

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.

Thanks for reading!!!
Next ⇒ Call by Value & Call by Reference in Java

⇐ Prev Next ⇒