Indefinite Arguments

When your program needs indefinite number of arguments, and let’s say,

i. You are not allowed to pass a collection or an array.
ii. You are not allowed to overload methods.

Using varargs is the best option to use in this case.

Before varargs was i…


This content originally appeared on DEV Community and was authored by lizardkingLK

When your program needs indefinite number of arguments, and let's say,

i. You are not allowed to pass a collection or an array.
ii. You are not allowed to overload methods.

Using varargs is the best option to use in this case.

Before varargs was introduced in Java 5, the problem was solved by allowing to use any of the mentioned two ways above.

A varargs method can pass indefinite number of arguments with the following method definition. (In here the data type is String and args is the name of the argument)

void someMethod (String... args)

Below is an use-case that used varargs to solve a problem which involved printing the sum of numbers.
Not allowing to use overloading or passing a collection or an array.
So we use varargs.

class Add {
    public void add(int... nums) {
        int sum = 0;
        String label = " ";
        for (Integer n: nums) {
            label += n + " ";
            sum += n;
        }
        label = label.trim().replace(" ", "+");
        label += String.format("=%d", sum);
        System.out.println(label);
    }
}

In the context of C# language we change the syntax a little bit.
We add the keyword 'params' to define it is a varargs method.

It looks like this.

void add(params int[] nums)

And the same code for above problem in C# language is as below.

class Add {
    public void add(params int[] nums) {
        int sum = 0;
        String label = " ";
        foreach (int n in nums) {
            label += n + " ";
            sum += n;
        }
        label = label.Trim().Replace(" ", "+");
        label += String.Format("={0}", sum);
        Console.WriteLine(label);
    }
}

Call to your method like below in your main method.
And again -> use any number of arguments.

public static void main(String[] args) {
    new Add().add(1, 2, 3);
    new Add().add(1, 2, 3, 4, 5);
}

Learn more about varargs using the following links.

https://www.baeldung.com/java-varargs


This content originally appeared on DEV Community and was authored by lizardkingLK


Print Share Comment Cite Upload Translate Updates
APA

lizardkingLK | Sciencx (2021-11-20T07:44:03+00:00) Indefinite Arguments. Retrieved from https://www.scien.cx/2021/11/20/indefinite-arguments/

MLA
" » Indefinite Arguments." lizardkingLK | Sciencx - Saturday November 20, 2021, https://www.scien.cx/2021/11/20/indefinite-arguments/
HARVARD
lizardkingLK | Sciencx Saturday November 20, 2021 » Indefinite Arguments., viewed ,<https://www.scien.cx/2021/11/20/indefinite-arguments/>
VANCOUVER
lizardkingLK | Sciencx - » Indefinite Arguments. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/20/indefinite-arguments/
CHICAGO
" » Indefinite Arguments." lizardkingLK | Sciencx - Accessed . https://www.scien.cx/2021/11/20/indefinite-arguments/
IEEE
" » Indefinite Arguments." lizardkingLK | Sciencx [Online]. Available: https://www.scien.cx/2021/11/20/indefinite-arguments/. [Accessed: ]
rf:citation
» Indefinite Arguments | lizardkingLK | Sciencx | https://www.scien.cx/2021/11/20/indefinite-arguments/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.