Java

What are Formal and Actual Parameters in Java

In programming, parameters are utilized to customize the behavior of functionality in order to adapt it to a particular requirement. In such an instance, the “formal” and “actual” parameters in Java come into effect. These parameters are of great aid in appending multiple functionalities or computations in the code, thereby making it (code) readable and streamlined.

This blog will elaborate on the implementation of the “formal” and “actual” parameters in Java.

What is a “Formal Parameter” in Java?

A “Formal parameter” corresponds to the value that is passed into the method by a caller. In simple words, it is the value acting as a reference to the passed value and is specified while defining a function.

Example 1: Applying “Formal Parameter” in Java

In this example, the formal parameter can be utilized to compute the multiplication of two numbers:

public static void multiply(Integer a, Integer b){
 System.out.println("The multiplication becomes: "+ a * b);
}

 

In this code, simply define a function named “multiply()” and specify the formal parameters “Integer a” and “Integer b” in it. In the function definition, return the multiplication of the parameters.

Note: The above code will not yield any outcome since the defined function is not invoked in the “main()” method.

What is an “Actual Parameter” in Java?

The “Actual parameter” aka argument indicates the actual value that is passed into the method by a caller. It is specified upon invoking the function.

Example 2: Applying “Actual Parameter” in Java

In this example, the utilization of the actual parameter, i.e., an argument can be demonstrated:

public static void main(String args[]){
 multiply(5,10);
}

 

Here, it invokes the defined function, i.e., “multiply()” in the former example and pass the actual parameters “5” and “10” in it.

Example 3: Applying Both the “Formal” and “Actual” Parameters in Java

In this particular example, both the “formal” and “actual” parameters can be applied to return the multiplication of the passed integers appropriately:

public class Formalactual {
 public static void multiply(Integer a, Integer b){
  System.out.println("The multiplication becomes: "+ a * b);
}
 public static void main(String args[]){
    multiply(5,10);    
}}

 

In the above code, simply integrate the above two examples by applying the following steps:

  • Define the function “multiply()” having the stated formal parameters.
  • In its definition, return the multiplication of the numbers.
  • Now, in the “main()” method, invoke the defined function by passing the specified actual parameters, i.e., “arguments”.
  • This will resultantly multiply the stated integers.

Output

In this outcome, it can be implied that the corresponding multiplication is returned.

Conclusion

A “formal” parameter corresponds to the value that is passed into the method by a caller. The “actual” parameter aka arguments indicate the actual value that is passed into the method by a caller. The former parameter is specified while defining a function and the latter parameter is placed upon invoking it(function). This blog demonstrated the implementation of the “formal” and “actual” parameters in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.