I'm just beginning to write programs in Java. What does the following Java code mean?
public static void main(String[] args)
What is String[] args
?
When would you use these args
?
Source code and/or examples are preferred over abstract explanations
In Java args
contains the supplied command-line arguments as an array of String
objects.
In other words, if you run your program as 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]);
}
}
}