Variable Arguments (Varargs) in Java

Hello coders, I am back with a new article series called Exploring Java. Here I am basically going to take java concepts and explain them one at a time and show you how and when to you them. I’ll try to keep the articles short and simple for you to grasp the core easily. Well, I hope you guys will like this article series. Let’s get to work.

What are Varargs?

Varargs aka Variable Arguments is a feature in Java that simplifies the methods that need to take in many arguments of the same type. Let me simplify. Instead of taking in arguments like this,

    public static int myMethod (int a, int b, int c, int d){
        return a + b + c + d;
    }

you can take in arguments like coded below.

    public static int myMethod (int ... args){
        System.out.println("Number of arguments : " + args.length);
        int sum = 0;
        for(int a: args){
            sum += a;
        }
        return sum;
    }

This code snippet will give the following result.

Number of arguments : 4
14

This is exactly like using an array as the argument. This is a more cleaner way to do it. I can pass any number of arguments of the same type. Here is how it works when there is a variable of another type.

    public static void main(String[] args) {
        System.out.println(myMethod("First One",1,3,4,6));
        System.out.println();
        System.out.println(myMethod("Second one"));
    }

    public static int myMethod (String text, int ... args){
        System.out.println("Number of arguments : " + args.length);
        System.out.println(text);
        int sum = 0;
        for(int a: args){
            sum += a;
        }
        return sum;
    }

This code snippets outputs the following result

Number of arguments : 4
First One
14

Number of arguments : 0
Second one
0

As you can see we can even stay without passing any parameters at all. This is a really cool feature for Java

Now you don’t have to create a new method every time an argument is added. You could just use varargs and not worry at all.


Thank you for reading my article. I hope you learned something valuable from it. If you did, drop a like and follow my blog to get notifications when I publish new articles. I write articles about coding, cricket and things that I find interesting. I try to publish articles every other day.

Have a nice day ✌

Published by Thisura Thenuka

I am a passionate software engineering student. But cricket is my first love ❤

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: