Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Core Java Basics. 1 Comment on Command Line Arguments
0
(0)

In this article we will learn about what a command line argument is and how to access and work with the command line arguments in Java.

 

Sometimes we might want to pass extra information while running a Java program. This extra information passed along with the program name are known as command line arguments. These command line arguments are separated by white spaces.

 

Command line arguments can be accessed using the string array specified in the main function’s signature. For example, if the array name is args, then the first command line argument can be accessed as args[0] and the second command line argument can be accessed as args[1] and so on.

 

Let’s look at the following Java program which access two command line arguments, adds them and displays the result to the user:


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


class CommandArgs
{
   public static void main(String[] args)
   {
      int x = Integer.parseInt(args[0]);
      int y = Integer.parseInt(args[1]);
      int sum = x + y;
      System.out.println("Sum of the command line arguments is: " + sum);
   }
}

 

Command line arguments are specified as shown in the below screenshot:

 

command-line-arguments

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

1 Comment

You can follow any responses to this entry through the RSS 2.0 feed.

It’s not working in netbeans IDE .
Please make a video on it in the netbeans IDE.

Leave a Reply

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