<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged arguments</title>
<link>https://coderlegion.com/tag/arguments</link>
<description>Powered by Question2Answer</description>
<item>
<title>Actual and formal argument lists differ in length</title>
<link>https://coderlegion.com/17789/actual-and-formal-argument-lists-differ-in-length</link>
<description>&lt;p&gt;&lt;strong&gt;
    Today, we're going to discuss a &lt;em&gt;compilation error&lt;/em&gt; in Java: &lt;span style=&quot;color: rgb(185, 106, 217);&quot;&gt;&lt;em&gt;&quot;Actual and formal argument lists differ in length&quot;&lt;/em&gt;&lt;/span&gt;. This error is produced when the number of arguments passed in doesn't match the number of parameters defined in the method or constructor. In this study blog, we'll explore what's &lt;span style=&quot;color: rgb(185, 106, 217);&quot;&gt;&lt;em&gt;&quot;Actual and formal argument lists differ in length&quot;&lt;/em&gt;&lt;/span&gt;, why it happens and how to resolve it.
  &lt;/strong&gt;&lt;/p&gt;
&lt;div id=&quot;toc-div&quot;&gt;
  &lt;h4&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;&lt;strong&gt; Table of Contents: &lt;a id=&quot;toc-toogle&quot; class=&quot;open&quot;&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/h4&gt;
  &lt;div id=&quot;toc-list&quot;&gt;
    &lt;ol&gt;
      &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#_Toc123774610&quot;&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;What is &quot;Actual and formal argument lists differ in length&quot;&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#_Toc123774611&quot;&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;Causes&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#_Toc123774612&quot;&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;Solution&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#_Toc123774613&quot;&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;The Conclusion&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#_Toc123774614&quot;&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;The Reference&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;section&gt;
  &lt;h2&gt;&lt;a class=&quot;custom_anchor&quot; name=&quot;_Toc123774610&quot; id=&quot;_Toc123774610&quot;&gt;&lt;/a&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;What is &quot;Actual and formal argument lists differ in length&quot;&lt;/span&gt; &lt;a id=&quot;TypeError-str-object&quot; style=&quot;color: blue;&quot; rel=&quot;nofollow&quot; href=&quot;#_Toc123774610&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h2&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
    First, let's understand what is &lt;em&gt;&lt;strong&gt;actual argument&lt;/strong&gt;&lt;/em&gt; and &lt;em&gt;&lt;strong&gt;formal argument&lt;/strong&gt;&lt;/em&gt;.
  &lt;/span&gt;&lt;/p&gt;
  &lt;ol&gt;
    &lt;li&gt;&lt;em&gt;&lt;strong&gt;Formal argument: &lt;/strong&gt;&lt;/em&gt; The arguments we &lt;strong&gt;define&lt;/strong&gt; for a method or constructor.&lt;/li&gt;
    &lt;li&gt;&lt;em&gt;&lt;strong&gt;Actual argument: &lt;/strong&gt;&lt;/em&gt; The arguments we &lt;strong&gt;pass into&lt;/strong&gt; a method or constructor.&lt;/li&gt;
  &lt;/ol&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
    Let's see an example:
  &lt;/span&gt;&lt;/p&gt;
  &lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;
  public static void main(String[] args) {
        int num_1 = 10;
        int num_2 = 20;
        int sum = addition(num_1, num_2);
        }
    
     public static int addition(int a, int b){
        return a + b;
    } 
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      From the above example, &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;a&lt;/em&gt; and &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;b&lt;/em&gt; is the &lt;strong&gt;formal arguments&lt;/strong&gt; while &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;num_1&lt;/em&gt; and &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;num_2&lt;/em&gt; is the &lt;strong&gt;actual arguments&lt;/strong&gt; that we pass into the method &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;addition&lt;/em&gt;.
  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      Now, we've understood the difference between &lt;em&gt;&lt;strong&gt;actual argument&lt;/strong&gt;&lt;/em&gt; and &lt;em&gt;&lt;strong&gt;formal argument&lt;/strong&gt;&lt;/em&gt;. Let's see another example:
  &lt;/span&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;
        public static void main(String[] args) {
        int num_1 = 10;
        int num_2 = 20;
        int sum = addition(num_1);
        }
    
     public static int addition(int a, int b){
        return a + b;
    }
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      The compilation error occurs:
  &lt;/span&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;
  method addition in class NewMain cannot be applied to given types;
  required: int,int
  found:    int
  reason: actual and formal argument lists differ in length
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
  Here, we pass in only one &lt;em&gt;actual argument&lt;/em&gt;, &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;num_1&lt;/em&gt; but the method, &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;addition&lt;/em&gt; actually defines two &lt;em&gt;formal arguments&lt;/em&gt;, &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;a&lt;/em&gt; and &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;b&lt;/em&gt; to work. Let's see another example:
  &lt;/span&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;
        public static void main(String[] args) {
        int num_1 = 10;
        int num_2 = 20;
        int num_3 = 30;
        int sum = addition(num_1, num_2, num_3);
        }
    
     public static int addition(int a, int b){
        return a + b;
    }
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      The compilation error also occurs:
  &lt;/span&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;
  method addition in class NewMain cannot be applied to given types;
  required: int,int
  found:    int,int,int
  reason: actual and formal argument lists differ in length
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
  Here, we pass in three &lt;em&gt;actual arguments&lt;/em&gt;, &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;num_1&lt;/em&gt;, &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;num_2&lt;/em&gt;, and &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;num_3&lt;/em&gt; while the method, &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;addition&lt;/em&gt; only need two &lt;em&gt;formal arguments&lt;/em&gt; to work.
  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
 So far we've discovered this problem in the method invocation. Let's see some examples of constructor:
  &lt;/span&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;
public class Student {
    private String first_Name;
    private String last_Name;
    
    public Student(String first_Name, String last_Name){
        this.first_Name = first_Name;
        this.last_Name = last_Name;
    }
}
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      Here, we declare a class, &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;Student&lt;/em&gt;, where we need &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;first_Name&lt;/em&gt; and &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;last_Name&lt;/em&gt; arguments in the constructor to create an instance of &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;Student&lt;/em&gt;.
  &lt;/span&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;
Student student = new Student(&quot;Alex&quot;);
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      Here is the compilation error:
  &lt;/span&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;
constructor Student in class Student cannot be applied to given types;
required: String,String
found:    String
reason: actual and formal argument lists differ in length
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      Let's move on to the next section to see why it occurs.
  &lt;/span&gt;&lt;/p&gt;
&lt;/section&gt;
&lt;section&gt;
  &lt;h2&gt;&lt;a class=&quot;custom_anchor&quot; name=&quot;_Toc123774611&quot; id=&quot;_Toc123774611&quot;&gt;&lt;/a&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;Causes&lt;/span&gt; &lt;a style=&quot;color: blue;&quot; rel=&quot;nofollow&quot; href=&quot;#_Toc123774611&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h2&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;From the above examples, we can see that this compilation error occurs when the number of &lt;em&gt;&lt;strong&gt;actual arguments&lt;/strong&gt;&lt;/em&gt; we pass into the method or constructor doesn't match the number of &lt;em&gt;&lt;strong&gt;formal arguments&lt;/strong&gt;&lt;/em&gt; we define for the method or constructor. As long as there's mismatch between the number of &lt;em&gt;&lt;strong&gt;actual arguments&lt;/strong&gt;&lt;/em&gt; and the number of &lt;em&gt;&lt;strong&gt;formal arguments&lt;/strong&gt;&lt;/em&gt;, this error will always be produced. Don't fret; it can be simply solved, let's move on to the next section:&lt;/span&gt;&lt;/p&gt;
&lt;/section&gt;
&lt;section&gt;
  &lt;h2&gt;&lt;a class=&quot;custom_anchor&quot; name=&quot;_Toc123774612&quot; id=&quot;_Toc123774612&quot;&gt;&lt;/a&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;Solution&lt;/span&gt; &lt;a style=&quot;color: blue;&quot; rel=&quot;nofollow&quot; href=&quot;#_Toc123774612&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h2&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
    You may guess the solution. Yes, you just need to remember the main concept while defining and using the methods / constructors:
      &lt;/span&gt;&lt;/p&gt;
    &lt;div style=&quot;margin-top: 30px; margin-bottom: 30px;&quot;&gt;
      &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0); padding: 15px;&quot;&gt;The actual and formal argument lists should always be &lt;strong&gt;same&lt;/strong&gt; in length&lt;/em&gt;
    &lt;/div&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
    What means? Before using the methods or constructors, you should check the number of formal arguments defined for a method or constructor. When you want to invoke a method or initiate an instance of object using constructor, make sure the number of actual arguments passed in is &lt;strong&gt;same&lt;/strong&gt; as the number of formal arguments.
      &lt;/span&gt;&lt;/p&gt;
  &lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;
public static void main(String[] args) {
        int num_1 = 10;
        int num_2 = 20;
        int num_3 = 30;
        int sum = addition(num_1, num_2, num_3);
        System.out.println(sum);
        }
    
     public static int addition(int a, int b, int c){
        return a + b + c;
    }
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      This is the output:
  &lt;/span&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;
60
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      We can either increase / decrease the number of formal arguments defined or the number of actual arguments passed in. Just to make sure both are having the same length.
  &lt;/span&gt;&lt;/p&gt;
&lt;div style=&quot;margin-top: 15px&quot; class=&quot;div-green&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;Tip&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt;For a Class, if you didn't override its constructor, the constructor has no any &lt;em&gt;formal arguments&lt;/em&gt;&lt;/span&gt;
&lt;/div&gt;
 &lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;
public class Student {
    private String first_Name;
    private String last_Name;

    public String getFirst_Name() {
        return first_Name;
    }

    public void setFirst_Name(String first_Name) {
        this.first_Name = first_Name;
    }

    public String getLast_Name() {
        return last_Name;
    }

    public void setLast_Name(String last_Name) {
        this.last_Name = last_Name;
    }
}
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      I define a class, &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;Student&lt;/em&gt; without overriding its &lt;em&gt;&lt;strong&gt;default constructor&lt;/strong&gt;&lt;/em&gt;.
  &lt;/span&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;
Student student = new Student();
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      The compilation error will not occur and we can assign value to the variable, &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;student&lt;/em&gt; using &lt;em&gt;setter&lt;/em&gt; method in the class.
  &lt;/span&gt;&lt;/p&gt;
&lt;/section&gt;
&lt;section&gt;
  &lt;h2&gt;&lt;a class=&quot;custom_anchor&quot; name=&quot;_Toc123774613&quot; id=&quot;_Toc123774613&quot;&gt;&lt;/a&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;The Conclusion&lt;/span&gt; &lt;a style=&quot;color: blue;&quot; rel=&quot;nofollow&quot; href=&quot;#_Toc123774613&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h2&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      Thanks for sticking with this blog until the end. In this lesson, we've learned about what's &lt;span style=&quot;color: rgb(185, 106, 217);&quot;&gt;&lt;em&gt;&quot;Actual and formal argument lists differ in length&quot;&lt;/em&gt;&lt;/span&gt; in Java. Besides, we've known what is the causes of this compilation error and how to solve it simply. Just to remind again, we should always make sure the &lt;em&gt;&lt;strong&gt;formal arguments&lt;/strong&gt;&lt;/em&gt; and &lt;em&gt;&lt;strong&gt;actual arguments&lt;/strong&gt;&lt;/em&gt; have the &lt;em&gt;&lt;strong&gt;same&lt;/strong&gt;&lt;/em&gt; length. Otherwise, this error will be thrown during compilation.
    &lt;/span&gt;&lt;/p&gt;
&lt;/section&gt;
&lt;section&gt;
  &lt;h2&gt;&lt;a class=&quot;custom_anchor&quot; name=&quot;_Toc123774614&quot; id=&quot;_Toc123774614&quot;&gt;&lt;/a&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;The Reference&lt;/span&gt; &lt;a style=&quot;color: blue;&quot; rel=&quot;nofollow&quot; href=&quot;#_Toc123774614&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h2&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      You may go through this reference for more information:
    &lt;/span&gt;&lt;/p&gt;
  &lt;ol&gt;
    &lt;li&gt;&lt;em&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;Constructors: &lt;/span&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html&quot; target=&quot;_blank&quot;&gt;https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html&lt;/a&gt;&lt;/em&gt;&lt;/li&gt;
  &lt;/ol&gt;
&lt;/section&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/17789/actual-and-formal-argument-lists-differ-in-length</guid>
<pubDate>Thu, 14 May 2026 05:55:50 +0000</pubDate>
</item>
</channel>
</rss>