Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » FundamentalRSS Feeds

Command Line Arguments

Posted By: Ida Andrews     Category: Java     Views: 17980

This article explains about Command-Line Arguments in java with examples.

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
 

  
Share: 

 
 

Didn't find what you were looking for? Find more on Command Line Arguments Or get search suggestion and latest updates.

Ida Andrews
Ida Andrews author of Command Line Arguments is from Wichita , United States. Ida Andrews says

 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!