[Solved] actual and formal argument lists differ in length

In this post, I will be sharing how to fix actual and formal argument lists differ in length error in Java. This error is a compilation error.

First, we need to understand the difference between the term's actual arguments and formal arguments with the help of an example. Formal arguments are defined in the definition of the method. In the below example, x and y are formal arguments. Actual arguments refer to the variables we used in the method call. In the below example, length and width are the actual arguments.

Read Also: IllegalArgumentException in Java with Examples

 public static int multiply(int x, int y)
{
    return x * y;
}

int width = 3;
int length = 2;
int area = multiply(length, width);

As always, first, we produce the actual and formal argument lists differ in length error before moving on to the solution. Let's dive deep into the topic:

[Fixed] actual and formal argument lists differ in length error

Example 1: Producing the error using methods


We can easily produce this error bypassing the different number of actual and formal arguments while calling the method as shown below in the example:
 public class ArgumentListDifferInLength {
    public static void main(String args[]) {
      /*Calling hello method and passing two arguments     
      whereas hello method is accepting three arguments, 
      hence the compilation error */
      hello(1,2);
    }
    public static void hello(int a , int b, int c) {
        System.out.println("hello");
    }
}

Output:
ArgumentListDifferInLength.java:6: error: method hello in class ArgumentListDifferInLength cannot be applied to given types;
              hello(1,2);
              ^
   required: int,int,int
   found: int,int
   reason: actual and formal argument lists differ in length
   1 error

Explanation:

The cause of this error is that we are calling the hello(int, int) method with two arguments but the compiler complains because it can only find the hello(int, int, int) method which has three arguments. There is a mismatch of the number of argument lists in length.

Solution:

Actual and formal argument lists should not differ in length. We can solve the above example by either passing three arguments in the actual argument as shown below in the example:

 hello(1,2,3);

or reducing the formal argument to two arguments as shown below in the example
 public static void hello(int a, int b) {


Example 2: Producing the error using constructors


We can easily produce this error by passing different number of actual and formal arguments while calling the constructor as shown below in the example:
 public class ArgumentListDifferInLength2 {
    public static void main(String args[]) {
      /*Calling ArgumentListDifferInLength2 class constructor and passing two arguments 10,23    
      where as ArgumentListDifferInLength2 constrcutor is accepting one argument "a" 
      hence the compilation error */
      ArgumentListDifferInLength2 var = new ArgumentListDifferInLength2(10,23);
    }
    public ArgumentListDifferInLength2(int a) {
        System.out.println("Alive is Awesome");
    }
}

Output:
ArgumentListDifferInLength2.java:6: error: constructor ArgumentListDifferInLength2 in class ArgumentListDifferInLength2 cannot be applied to given types;
              ArgumentListDifferInLength2 var = new ArgumentListDifferInLength2(10,23);
                                                                             ^
    required: int
    found: int,int
    reason: actual and formal argument lists differ in length
1 error

Explanation:

The cause of this error is that we are calling the ArgumentListDifferInLength2(int, int) constructor with two arguments but the compiler complains because it can only find the ArgumentListDifferInLength2(int) constructor which has one argument.

Solution:

Actual and formal argument lists should be the same in length. We can solve the above example by either passing one argument in the actual argument as shown below in the example:

 ArgumentListDifferInLength2 var = new ArgumentListDifferInLength2(10);

or increasing the formal argument to two arguments as shown below in the example
 public ArgumentListDifferInLength2(int a, int b) 

That's all for today, please mention in the comments in case you still face the compilation error actual and formal argument lists differ in length.

Reference: Toronto edu

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry