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.
In some cases a program can be run in different ways and exhibit different behavior on how it is called. If you call vim as vi, it runs in a compatibility mode. Sometimes it is to try to maintain one version of several related programs - for example mailq and newaliases on many unix systems are a link to sendmail so that these programs stay in sync)
Java programs are typically invoked as:
% java -jar foo.jar args % java Foo args
The first version is where you have a Manifest file that indicates the main class, the second version runs the main method in the class Foo found in the class path.
The information presented for Java is either a path to the jar or the name of the class being invoked.
The location of the jar isn't important enough to be something to code from (and was actually not part of the original spec). A Jar can be named anything really, and often includes version numbers. Whats more, there's no guarantee that the class was even stored in a .jar (it could have been extracted).
Invoking a Java application with -jar has only one way to enter it - the class defined in the Manifest. There's no renaming that can be done.
The other option, of invoking it with the class name points directly to the execution unit. Furthemore, it can't be named multiply - you can't have Bar.class be the code for class Foo it just doesn't work that way.
This should show that there's really no point to passing the information of argv[0] in the C sense to a Java application - its either going to be java, meaningless and arbitrary, or the name of the class that is being invoked (that you are already executing code out of (you could do something like getClass().getEnclosingClass().getName() if you were desperate...)).
There is a point here, you can define multiple Main methods in classes in a .jar or on the class path. And you could have them behave differently just as if there was a series of if statements based on what argv[0] was.
I have in the past had code akin to java -cp Foo.jar com.me.foo.Test which invoked the Test class's Main method rather than the one defined in the one defined in the Manifest.
actually there's no benefit with it, it really depends on the syntax of the programming language you are using if it is 0-based or 1-based. the variable (you refer to as filename) also depends on the language, it can be different in other languages just follow the correct syntax of the language you are using.