When you execute the program, you specify the class that contains the main method and then command line arguments. args[0] is the first command line argument. You need to provide it when you run the program.
java -cp . com.something.Main A B C
where A is args[0], B is args[1] and C is args[2].
A brief explanation or general overview would be great! Can I use it it to input a large number of data into a collection?
Hello Everyone,
I have encountered this type of code
public static void main ( String [] args)
// And immediately
int n = Integer.parseInt(args[0]);
// In here what does args[0] mean? // are we just supposed to use args[] in static void main
Args is the name of the string array brought in by the "main" function. It holds whatever you wrote on the command line when running the program. Something like:
/myprogram 1
would have "1" as args[0]
Integer.pareInt assumes args[0] is a number and casts it appropriately.
You can pass String arguments via the command line to the main method when you execute the program. It’s just saying then parse the first String in the array
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.
The args parameter is passed to the main method when the application is run. If no arguments are passed to the application accessing the first index of the args array will throw an ArrayIndexOutOfBoundsException.
Try running your application from the command line as follows:
cd /Whatever/directory/contains/the/class
javac Test.java
java Test a b c
If you are using the Eclipse IDE program arguments can be set by Run > Run Configurations and then selecting your program and supplying arguments.

Run your program like
java Test foobar
foobar is arg[0].
You should check array sizes, though.