Scanner class Methods in Java

Java - Command Line Arguments

 What are Command Line Arguments?

You might have attended some coding challenges which ask you to use command line arguments for I/O operations.In this post , I want you to understand what are command line arguments and how we can use them.

When you run a Java program from Command prompt, you can input arguments that are passed to main method of class. In main() method we pass a String array as an argument to Java program.This String array is known as Command Line Argument.This is essential argument to pass information into a program at runtime.

Passing Command line argument is pretty simple. You can pass arguments using Command Prompt at run time. Use following syntax to pass arguments:

In cmd write,

java className arg1,ar2,arg3..............argN

for example,

> java FirstProgram Hello, World!

The java className is the command to run Java file in Command Prompt. And at the end you can pass any number of arguments. Do not get perplexed ! I will help you in understanding.

Use of Command Line Arguments

Lets see a quick example to understand the use of Command Line Arguments.In this example, we will make a simple program which will print the arguments that we pass at runtime in Command Prompt.

Write a program to show the use of Command Line Arguments and print them on screen.

What is Command Line Argument?
Example of Command Line Arguments in Java

Write above program in Notepad, If you do not know how to write program in Notepad and run using Command Prompt. Just visit  Writing first program in Java using Notepad. You can find the important commands of Cmd on that post.

Explanation: In the above program,

  1. We created a class "CommandLineArgExample".
  2. In the main method, we are passing String[] args as we always do.
  3. In the body of  main, we are using three print statements to print the values saved in the "args" string array.
  4. We can print the element of an array using its index. So, In the print statement we have "args[0]" which will print first argument.Similarly index 1 and 2 will print second and third argument.In java, array index starts at 0. So there are three elements but java will count them from 0 to 2.
  5. Check output to see how arguments are passed to main method.
Output:

Command Line Arguments Example
                               Passing Command Line Arguments in Command Prompt

The command to pass arguments and arguments both are highlighted with blue color and the output of program is highlighted with red color.

I have passed all types of arguments in the output. Java treats all arguments of different data types like int , float, char and String as String type. The arguments are distinguished by spaces between them. Three print statements prints 3 arguments passed in java command.

Where these arguments are stored ?

The command line arguments are passed into the main method by JVM and placed in the args parameter which is array of strings.To access theses arguments within main() method we use array name (args) with square brackets and index passed in square brackets as we used in above example.

If you enjoyed this post, leave a comments.Thanks for reading. Happy Coding!
  





Comments