Java Command Line Arguments

Here you will learn about Java command line arguments.

The arguments that are passed from command line at the time of running the program are called command line arguments.

It is one of the ways to take input from user in java. It works only when we are running the program from command line and not from any IDE.

 

How to take input from command line in Java?

Write the values that you have to input just after the name of the class that is to be run. Suppose we want to run class Demo and give hello as an input then it can be done as.

 

In this way you can input or read n number of values separated by space.

The input that we give is stored as String array in args parameter of main() method. To access these values you have to access the args array.

Below example shows how to do this.

 

Java Command Line Arguments Example

Lets make one program in which we will input two numbers from command line and calculate their sum.

 

Output

Java Command Line Arguments

 

The values that are passed through command line are of String type so first we need to convert them into integer before using.

 

If you have any queries then feel free to ask by commenting below.

Leave a Comment

Your email address will not be published. Required fields are marked *