![]() |
![]() |
Java if statement | |
Java if-else statement | |
Java nested if-else | |
Java else-if ladder | |
Java Switch case statement | |
Java Nested Switch Case | |
Java Ternary/Conditional operator |
Java while loop | |
Java do-while loop | |
Java for loop | |
Java Enhanced loop/for each loop | |
Java Labelled for loop | |
Java Nesting of loops | |
Java Break and Continue |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions | |
command line arguments are parameters that are supplied to the application program at the time of starting its execution. They must be supplied at the time of its execution after the filename.
we can pass any number of commandline arguments while launching an application
Normally we execute the program by using below commandsEverything you pass from commandline argument is considered as inString
format.
Lets see below program to understand best use of commandline argument
Qn. Develop a program to print string using commandline argument
class First { public static void main(String args[]) { System.out.println("You have passed : "+args[0]); } }
Compile above program by using following steps
By default the arguments are considered as String. We have need to convert them into the respective datatype by using type casting.
If you want to store integer value into variable we use int a = Integer.parseInt(arg[0]);
Develop a program to find addition of two numbers using commandline argument.
NOTE : Assume values are 10 and 20 |
class Addition { public static void main(String args[]) { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); System.out.println("A : "+args[0]); System.out.println("B : "+args[1]); System.out.println("Addition = "+ a+b); } }
Compile the program using
any
number string arguments because we have not specified any size like args[10] etc.args[0]
index and second value is in args[1]
. thats why we have specified the indexes of each value while converting into integer.parseInt()
: which is used for conversion of string into integer typejava allows us to convert the string into following datatypes