0

Let's see the following code snippet in Java.

package common;

final public class Main
{
    private static void show(Object... args)    //<--Here it is...
    {
        for(int i=0;i<args.length;i++)
        {
            System.out.println(args[i]);
        }
    }

    public static void main(String[] args)
    {
        show(1, 2, 3, 4, 5, 6, 7, 8, 9);
    }
}

The above code in Java works well and displays numbers starting from 1 to 9 through the only loop on the console. The only question here is the meaning of (Object... args) in the above code.

2 Answers 2

6

The three-dot notation is the syntax for variable number of arguments, take a look here.

Sign up to request clarification or add additional context in comments.

Comments

2

You're using Java's varargs notation, which allows the final argument to be passed as either an array or sequence of arguments (of indeterminate length). In your case, you're passing them as a sequence of args:

show(1, 2, 3, 4, 5, 6, 7, 8, 9);

...but you could also pass them like this:

show(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9});

Without support for this feature, you'd either have needed to accept an array in the method signature (and always passed the inputs in an array) or specified a fixed number of int arguments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.