Java Command Line Argument
Command-line arguments are passed to the main() method when we want to pass information into a program during runtime.
It is the information that directly follows the program’s name on the command line when it is executed.
How to use a command-line argument in a Java program?
It is quite easy to use a command-line interface in a Java program. They are stored as strings in a “String” array passed to the "args" parameter of "main()". Let’s understand it with a simple program:
Class CommandLineDemo { Public static void main(String args) { System .out.println("First argument is:"+ args[0]); } }
Write the below command in Command Line Interface
To compile- javac CommandLineDemo.java
Run by- java CommandLineDemo Tutorialandexample.com
Output:
Tutorialandexample.com
Note: All command-line arguments can be passed as a string. We must convert numeric values into their internal form manually.
An example to illustrate the working of command-line arguments.
Class Demo { Public static void main(String args[]) { for(inti=0;iWrite the below codes in Command Prompt
javac Demo.java java Demo Ankit Kumar Shahi Output: args[0]:Ankit args[1]:Kumar args[2]:Shahi