This seems to be always included in java's main method, but I still don't understand what it means honestly.
Edit: thanks!
In Java args contains the supplied command-line arguments as an array of String objects.
In other words, if you run your program in your terminal as :
C:/ java MyProgram one two
then args will contain ["one", "two"].
If you wanted to output the contents of args, you can just loop through them like this...
public class ArgumentExample {
public static void main(String[] args) {
for(int i = 0; i < args.length; i++) {
System.out.println(args[i]);
}
}
}
The program will print in the terminal:
C:/ java MyProgram one two
one
two
C:/
Those are for command-line arguments in Java.
In other words, if you run
java MyProgram one two
Then args contains:
[ "one", "two" ]
public static void main(String [] args) {
String one = args[0]; //=="one"
String two = args[1]; //=="two"
}
The reason for this is to configure your application to run a particular way or provide it with some piece of information it needs.
If you are new to Java, I highly recommend reading through the official Oracle's Java Tutorials.
Videos
(String ...) is an array of parameters of type String, where as String[] is a single parameter.
Now here String[] can full fill the same purpose here but (String ...) provides more readability and easiness to use.
It also provides an option that we can pass multiple array of String rather than a single one using String[].
A feature of String[] vs String... is that the "String..." does not need to be part of the call.
public void test(String... args){
if(args.length > 0){
for( String text : args){
System.out.println(text);
}
}else{
System.out.println("No args defined.");
}
}
public void callerTest(){
test();
System.out.println();
test("tree");
System.out.println();
test("dog", "cat", "pigeon");
}
Then if you call callerTest(); the Output will be:
No args defined.
tree
dog
cat
pigeon
The only difference between the two is the way you call the function. With String var args you can omit the array creation.
public static void main(String[] args) {
callMe1(new String[] {"a", "b", "c"});
callMe2("a", "b", "c");
// You can also do this
// callMe2(new String[] {"a", "b", "c"});
}
public static void callMe1(String[] args) {
System.out.println(args.getClass() == String[].class);
for (String s : args) {
System.out.println(s);
}
}
public static void callMe2(String... args) {
System.out.println(args.getClass() == String[].class);
for (String s : args) {
System.out.println(s);
}
}
The difference is only when invoking the method. The second form must be invoked with an array, the first form can be invoked with an array (just like the second one, yes, this is valid according to Java standard) or with a list of strings (multiple strings separated by comma) or with no arguments at all (the second one always must have one, at least null must be passed).
It is syntactically sugar. Actually the compiler turns
function(s1, s2, s3);
into
function(new String[] { s1, s2, s3 });
internally.
String[] result = Stream.of(args)
.flatMap(a -> Stream.of(a.split("=")))
.toArray(String[]::new);
The other answers are all fine technically, but the real answer is: don't.
Do not re-invent the wheel. Parsing command line options is actually hard. Whatever you come up with works for the first step, but assuming that we are talking about something that is intended to last, and good enough to attract users - then sooner or later, you spent more and more time on dealing with the options.
Thus: instead of doing any of this yourself (which is of course a bit of fun) accept that parsing command line options is a solved problem. Simply get one of the existing solutions, see here for starters.
It's a String because the command line is expressed in text. If you want to convert that text into integers or booleans, you have to do that yourself - how would the operating system or Java bootstrapper know exactly how you wanted everything to be parsed? I suppose Java could look for a main method with the same number of parameters as command line arguments, and then try to parse them using Integer.parseInt etc... but this would be pretty complicated, and would still be inadequate in many situations.
As for it being mandatory - basically the Java designers decided to stick to a single method signature, which is frankly simpler than allowing for it to be optional. It would be possible though - in C#, you can have any of
static void Main()
static void Main(string[] args)
static int Main()
static int Main(string[] args)
Ultimately it doesn't make a lot of difference, to be honest. There's no significant downside in having to include the parameter.
The Java runtime system looks specifically for a method with a single String[] type parameter, because it wants to pass the parameters to your main method. If such a method is not present, it informs you through an exception.
If you want to treat the (string) command line parameters as integers or booleans, you are expected to do the conversion yourself. This allows you to handle the condition where somebody enters "ponies" where you expect an integer.