All java applications contain a static method named main(). It takes one argument that is an array of String objects. These objects represent any arguments that may have been entered by the user on the command line.
The number of command line arguments is obtained by the expression args.length. It is of type int. We can access individual arguments by args[0], args[1] and so on...
Example of Command line arguments
Example 1 : Program that reads and displays command line arguments
class CommandLineArguDemo
{
public static void main()
{
System.out.println("Command line arguments length is " + args.length);
System.out.println("Array index 0 value is " + args[0]);
System.out.println("Array index 1 value is " + args[1]);
}
}
// Call above application from the command line as follows :
java CommandLineArguDemo Hello world
Output
Command line arguments length is 2
Array index 0 value is Hello
Array index 1 value is world