What is String args[ ] in Java?
Introduction
String args[] in java is an array of type java.lang.String class that stores java command line arguments. Here, the name of the String array is args (short form for arguments), however it is not necessary to name it args always, we can name it anything of our choice, but most of the programmers prefer to name it args.
We can write String args[] as String[] args as well, because both are valid way to declare String array in Java.
From Java 5 onwards, we can use variable arguments in main() method instead of String args[]. This means that the following declaration of main() method is also valid in Java : public static void main(String... args), and we can access this variable argument as normal array in Java.
Why is String... args[] in Java Needed?
Varargs or Variable Arguments in Java was introduced as a language feature in J2SE 5.0. This feature allows the user to pass an arbitary number of values of the declared date type to the method as parameters (including no parameters) and these values will be available inside the method as an array. While in previous versions of Java, a method to take multiple values required the user to create an array and put those values into the array before invoking the method, but the new introduces varargs feature automates and hides this process.
The dots or periods(...) are known as ELLIPSIS, which we usually use intentionally to omit a word, or a whole section from a text without altering its original meaning. The dots are used having no gap in between them.
The three periods(...) indicate that the argument may be passed as an array or as a sequence of arguments. Therefore String… args is an array of parameters of type String, whereas String is a single parameter. A String[] will also fulfill the same purpose but the main point here is that String… args provides more readability to the user. It also provides an option that we can pass multiple arrays of String rather than a single one using String[].
Example to Modify our Program to Print Content of String args[] in Java
When we run a Java program from command prompt, we can pass some input to our Java program. Those inputs are stored in this String args array. For example if we modify our program to print content of String args[], as shown below:
The following Java Program demonstrates the working of String[] args in the main() method:
How to Run:
Compile the above program using the javac Test.java command and run the compiled Test file using the following command:
Output :
Explanation:
In the above example, we have passed three arguments separated by white space during execution using java command. If we want to combine multiple words, which has some white space in between, then we can enclose them in double quotes. If we run our program again with following command:
Output:
Conclusion
- string args[] in java is an array of type java.lang.String class that stores java command line arguments.
- Variable argument or varargs in Java allows us to write more flexible methods which can accept as many arguments as we need
- The three periods(... ) in String… args[] indicate that the argument may be passed as an array or as a sequence of arguments.
- (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.