Command Line Arguments
Lecture 7
Department of Computer Science and Engineering, ITER
Siksha ’O’ Anusandhan (Deemed to be University), Bhubaneswar, Odisha, India.
1 / 10
Contents
1 Java Program to Add two Numbers
2 Command Line Arguments
3 Passing Numeric Command-Line Arguments
2 / 10
Java Program to Add two Numbers
Write program to add two numbers.
p u b l i c c l a s s AddTwoNumbers {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] args ) {
i n t num1 = 5 , num2 = 15 , sum ;
sum = num1 + num2 ;
System . o u t . p r i n t l n ( ” Sum = : ”+sum ) ;
}}
Output:
Sum = : 20
If you want to pass inputs to a program when you run it.
This is accomplished by command-line arguments. 3 / 10
Command Line Arguments
The command-line arguments in Java allow the programmers to
pass the arguments during the execution of a program.
A command-line argument is the information that you pass after
typing the name of the Java program during the program
execution.
java MyProgram Welcome to Odisha
You can pass both Strings and primitive data types.
These arguments convert into a String array(args[ ]) and provided
to the main() function.
JVM stores the first argument at args[0], the second at args[1],
and so on.
4 / 10
Example - 1
c l a s s CommandLine{
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] args ) {
System . o u t . p r i n t l n ( args [ 0 ] ) ;
System . o u t . p r i n t l n ( args [ 1 ] ) ;
System . o u t . p r i n t l n ( args [ 0 ] + args [ 1 ] ) ;
}}
At execution: java CommandLine Good Morning
Output:
Good
Morning
GoodMorning
5 / 10
Example - 2
But if you execute this program, with the following numeric inputs:
java CommandLine 10 20 30
The output will be:
10
20
1020
Remember, the main() method of every Java program only
accepts string arguments.
So, the plus signs (+) concatinates ”10”, and ”20” to form ”1020”
as the output.
6 / 10
Passing Numeric Command-Line Arguments
You can never pass numeric arguments through the command
line. Instead you convert string arguments into numeric values.
p u b l i c c l a s s NumericArguments {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] args ) {
/ / convert i n t o i n t e g e r type
i n t num1 = I n t e g e r . p a r s e I n t ( args [ 0 ] ) ;
System . o u t . p r i n t l n ( ” F i r s t Number : ” +num1 ) ;
i n t num2 = I n t e g e r . p a r s e I n t ( args [ 1 ] ) ;
System . o u t . p r i n t l n ( ” Second Number : ” +num2 ) ;
i n t r e s u l t = num1 + num2 ;
System . o u t . p r i n t l n ( ” Sum is : ” +result ); }}
7 / 10
Numeric Command-Line Arguments Example
Now if you execute this program, as shown here:
j a v a c NumericArguments . j a v a
j a v a NumericArguments 10 20
The output will be:
F i r s t Number : 10
Second Number : 20
Sum i s : 30
8 / 10
Numeric Command-Line Arguments
In the above example, notice the line,
i n t num1 = I n t e g e r . p a r s e I n t ( args [ 0 ] ) ;
Here, the parseInt() method of the Integer class converts the
string argument into an integer.
Similarly [Link]()converts the string argument into
a double.
And [Link]()converts the string argument into a float.
9 / 10
References
Y Daniel Liang ‘Introduction to JAVA Programming, Comprehensive Version’,
Pearson, 2014.
10 / 10