Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

Command-Line Arguments in Java programming language


Introduction :-

Hello friends, having question in mind, How to use Command-Line Arguments in Java ?. In this hub I will be teaching you how to utilize java command-line utilities?. Learning Java by example is the most effective proven technique. Java fundamentals are necessary in order to get an idea for writing a efficient code. Our program code in this hub will require Java's basic knowledge such as : How to write your first Java program ?. and how to create, compile and run a java program ?.




Click here to download complete source code 

Command-Line Argument Explanation :-
 
A normal Java application before running, can accept multiple command line arguments from the programmer via console or IDE runtime configuration. The arguments are specified by the user at the command line which are required by the Java applications. The user enters command line arguments while launching the program by java command. This passing of arguments by java command is optional. The main method gets arguments from command line and stores it in an array of String. The array elements are initialized by whatever user types after class-name in java command. Let us take an example, suppose we want to launch a class say Hubpages.java with command line arguments, we type command after compilation as :

java Hubpages I am hubber
 
Here Hubpages is the name of class we want to launch, "I", "am", "hubber" would be three String passed to program's main method, which will store all three in the form of array of Strings. Generally, space between I, am and hubber makes them separate strings. If we want them to be one string we use double quotes "". In our example we can pass it as one single string as "I am hubber". Let us look at the below sample code and try to figure out what would be the output.

Program to demonstrate working of Command-Line Arguments

public class Argument {
   public static void main(String[ ] args) {
        System.out.print("Hi, ");
        System.out.print(args[0] + " " );
        System.out.print(args[1] + " " );
        System.out.println(". How are you?");
    }
}


Output and Explanation of working of above sample code :-

When we run above program by command say : java Argument Java Programs We get the following output on the console window:
Hi, Java Programs . How are you?
Here we pass two command-line arguments "Java" and "Programs". These two strings are passed as an argument to main method and gets stored in the form of an array. Where args[0] is assigned "Java" value and args[1] is assigned "Programs" value. These values are printed on console by System.out.print method.

Parsing Numeric Command-Line Arguments :- 

Suppose the programmer wants to enter a numeric value on command-line and let say for an example he inputs 10. Now the problem in this case is that though he had input value as 10 which is a numeric value of data type int, main method will store it as a string which is equal to "10". In order to use numeric value in program we need to parse string tointeger value. To do parsing we use Integer class which has method called as parseInt. This is static method and has argument which is a String. This method takes the String value and parses to numeric type which we can use in our program numerically. Let us look into through sample code below :

public class Argument {
   public static void main(String[ ] args) {
        
       int number1 = Integer.parseInt(args[0]);
       int number2 = Integer.parseInt(args[1]);


       int sum = number1 + number2;
       
       System.out.print("The sum of two number is : " + sum);
       
    }
}


Output and Explanation of working of above sample code :-

When we run above program by command say : java Argument 10 20 We get the following output on the console window:
The sum of two number is : 30
Here we pass two command-line arguments "10" and "20". Remember these two values are strings and they need to be first convert into numeric value. For this we have used Integer class method called as parseInt. It takes string and parses it to integer value. If value provided to this method is not a number, we get an exception such as NumberFormatException.
 
© 2021 Learn Java by Examples Template by Hubberspot