Command Line Arguments in Java

Java allows you to pass some details to a program from outside at the time of running the program. This is done through command line arguments.


1. Syntax

You have to type java then program-name then all the arguments are typed. Each argument is separated by a blank space.


2. An Example

Command line arguments are stored in the String array args. You can rename this array to whatever name you want. But the important thing is: JVM passes the command line arguments as an array of Strings to the main method.  This array name can be any valid java identifier.

You can run it as:

 


3. Quick Notes

  • There is no limit to number of arguments you can pass to a program.
  • Whatever you type after the program name, everything is passed as an string to the program.
  • JVM packs these strings in a string array and passes this array to the main method.
  • Blank space is used to separate two command line arguments.

4. Miscellaneous Topics

  • Use double quotes, if a single argument has blank space inside it.

    You can run it like:


  • The command line arguments are read as string by the JVM. If you need other data types, you have to convert them first.

    You can run it like :