Oh! Whats this ‘…’ in public static void main(String … args) in Java?

Oh! Whats this ‘…’ in public static void main(String … args) in Java?

Image for post

Java programmers have a very close connection to writing the main function as public static void main(String args). Those who have been working with it for some time have got into depth and know what that statement in the parenthesis means.

To those who don?t know it yet can also guess if you got your basics right. Its a String array and it names as ?args?(short form for argument). Well, that wasn?t a hard nut to crack. But what if I ask you to guess what does ?String ? args? could mean, what could all the possibilities be that you could think of?

Image for postIs that what you feel?

Rather than directly coming over the exact meaning, let me tell you something about ? ELLIPSIS.

ELLIPSIS is a series of dots or period(???) that are used in almost all languages (No! Not the programming languages, the general languages- English, German and so on) which we usually use intentionally to omit a word, sentence, or a whole section from a text without altering its original meaning. The dots are used having no gap in between them. An example is English, we say: It is not cold? it is freezing cold.

The same is used in programming languages and also in mathematics. Example: In mathematics, it is used as 1+2+3+?+100 and we know it at a glance that the dots represent rest of the missing numbers . Looks a bit familiar, isn?t it? Ellipsis is used in numerous programming languages starting from Pascal, C to Python and to C++, Go and Java. In C, C++, and Java, it is used as a Variadic function, a function of an indefinite number of arguments.

Varargs or Variadic function in Java was introduced as a language feature in J2SE 5.0. The argument of the method can be declared as a variable arity parameter or simply varargs method. This allows one to pass a variable number of values of the declared type to the method as parameters(including no parameters) and these values will be available inside the method as an array.

void printNumbers(int… numbers) { //numbers represents varargs for (int num : numbers) { System.out.println(num); }}// Calling varargs methodprintNumbers(1,2,3,4,5);

The output would be the numbers displayed as one per each line.

But why was it needed?

In Java?s past releases, a method to take an arbitrary number of values required to create an array and put the values into the array prior to invoking the method. It is still true that multiple arguments must be passed in an array, but the varargs feature automates and hides the process. Furthermore, it is upward compatible with preexisting APIs. The three periods indicate that the argument may be passed as an array or as a sequence of arguments.

Hence (String? args) is an array of parameters of type String, whereas String is a single parameter. String can full fill the same purpose but just (String? args)provides more readability and easiness to use. It also provides an option that we can pass multiple arrays of String rather than a single one using String. It can be used when the programmer wishes to you Varargs as per the program requirements.

If you found the blog useful, show your love by ? .

1

No Responses

Write a response