What is "String args[]"? parameter in main method Java
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]); } }}
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.
args
contains the command-line arguments passed to the Java program upon invocation. For example, if I invoke the program like so:
$ java MyProg -f file.txt
Then args
will be an array containing the strings "-f"
and "file.txt"
.