Command Line Arguments

Define: When a java program is launched from a terminal window, you can provide the program with either zero or more command-line-arguments.
Normally all values which user passes at the time of executing any java program, these values get stored in an array, “String args[]”.

Program:

public class CommandLineDemo
{
	public static void main(String args[])
	{
		System.out.println();
		for(int i=0; i<args.length; i++)
		{
			System.out.println("Value" +i+ "=" +args[i]);
		}
	}

}

Output:

Explanation: So here in above cmd snapshot

    javac CommandLineDemo.java 
// user has first compiled the java program

    java CommandLineDemo 1 2 3 
// then during execution, user passes 3 values as 1,2,3

Now here

for(int i=0; i<args.length; i++) 
// we are running "for loop" till the length of array, which is here 3 (as 3 values passed by user in CMD)
{ 	System.out.println("Value" +i+ "=" +args[i]); 
//Printing all 3 values of array, one by one
}

Thus the output is

Value 0=1
Value 1=2
Value 2=3
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s