Command Line Arguments In Java
Command Line Arguments In Java
Command Line Arguments In Java
Before we dive in to Command Line Arguments In Java , let’s first explore various way of accepting inputs from the user . In Java there are 3 ways to accept input from user :
→ Through Command Line Arguments
→ Using Scanner classes
→ Using GUI Components
In this post we will cover command line arguments and the other two methods will be covered in future Posts.
Command Line Arguments In Java
» Using command line arguments we can accept input when we are about to execute the program .
» This is where the arguments of method main() comes in action.
» Let us take an example to understand this…
Case 1:
class Test { public static void main(String [ ] args) { System.out.println(“Hello ”+args[0]); } }
Case 2:
Case 3:
Case 4:
Case 5:
Case 6 ( Passing Integers )
class AddNos { public static void main(String [ ] args) { System.out.println("First number is "+args[0]); System.out.println("Second number is "+args[1]); System.out.println("Their sum is "+args[0]+args[1]); } }
Output
C:\Program Files\Java\jdk1.8.0_66\bin>javac AddNos.java C:\Program Files\Java\jdk1.8.0_66\bin>java AddNos 10 20 First number is 10 Second number is 20 Their sum is 1020 C:\Program Files\Java\jdk1.8.0_66\bin>
Why was the output 1020 ?
» Because anything which we pass from “Command prompt” is by default treated as a String by java.
» Now since java is considering the values 10 and 20 as “10” and “20” , so the operator + concatenated them instead of adding them mathematically.
» To solve this problem we have to convert the values “10” and “20” from String to int and this is done using special classes in java called “Wrapper Classes”
Wrapper Classes
» In java , corresponding to 8 primitive data types we have 8 predefined classes also , called “Wrapper Classes”.
» These classes are available in the package java.lang and have their names similar to the name of data type.
For ex: Integer, Character, Float, Boolean etc
Notice that the first letter in wrapper class name is in uppercase while in case of data type name it is in lowercase.
For ex: byte and Byte, long and Long and so on.
Uses Of Wrapper Classes
Wrapper classes are mainly used for two purposes:
» To represent primitive data types as objects.
» To convert String form of a primitive value to it’s original form, for ex: “10” to 10
Representing primitives as objects
Consider the following statement:
int a=10; // variable a
Here a is a variable initialized to 10
But if we want we can convert it into an object by using the Wrapper Class Integer, as shown below:
Integer obj=a; // a converted to object
Converting String To Primitive
» Another importance of wrapper classes is that they contain special methods which perform conversion from String to primitive data type.
» These methods have their name as parseXXX where XXX is the name of primitive type.
» Also they are static in nature, so they can be directly called by their class name.
For Example :- Integer.parseInt(“……”);
List Of Wrapper Classes
Data Type | Wrapper Class | Range |
int | Integer | parseInt() |
short | Short | parseShort() |
byte | Byte | parseByte() |
long | Long | parseLong() |
float | Float | parseFloat() |
double | Double | parseDouble() |
char | Character | No parse method available |
boolean | Boolean | parseBoolean() |
Addition Of Number Using Wrapper Classes
class AddNos { public satic void main(String args[]) { int a,b,c; a=Integer.parseInt(args[0]); b=Integer.parseInt(args[1]); c=a+b; Syetem.out.println(“First number is ”+a); Syetem.out.println(“Second number is ”+b); Syetem.out.println(“Their sum is ”+c); } }
Output
C:\Program Files\Java\jdk1.8.0_66\bin>javac AddNos.java C:\Program Files\Java\jdk1.8.0_66\bin>java AddNos 10 20 First number is 10 Second number is 20 Their sum is 30 C:\Program Files\Java\jdk1.8.0_66\bin>
Guess the output ?
class AddNos { public satic void main(String args[]) { int a,b,c; a=Integer.parseInt(args[0]); b=Integer.parseInt(args[1]); c=a+b; Syetem.out.println(“First number is ”+a); Syetem.out.println(“Second number is ”+b); Syetem.out.println(“Their sum is ”+c); } }
During the execution if we pass integer value as a first input and String as a second input
Running:
bin> java AddNos 10 Varanasi
Output:
C:\Program Files\Java\jdk1.8.0_66\bin>javac AddNos.java C:\Program Files\Java\jdk1.8.0_66\bin>java AddNos 10 Varanasi Exception in thread "main" java.lang.NumberFormatException: For input string: "Varanasi" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at AddNos.main(AddNos.java:7) C:\Program Files\Java\jdk1.8.0_66\bin>
Why did Exception occur ?
» Because the method parseInt( ) can only work with Strings containing digits.
» If any String contains non integer values then the method parseInt( ) will throw Exception.
» Even if we pass 20.5 , then also it will throw “NumberFormatException”
Output:
C:\Program Files\Java\jdk1.8.0_66\bin>java AddNos 10 20.5 Exception in thread "main" java.lang.NumberFormatException: For input string: "20.5" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at AddNos.main(AddNos.java:7) C:\Program Files\Java\jdk1.8.0_66\bin>
How to accept decimal values?
» So if we want to accept decimal values , then we must use the method parseFloat( ) or parseDouble( ).
» They accept decimal/integer both kinds of values and throw Exception only if the given values is non-numeric like “bhopal”, “10a” etc
Command Line Arguments In Java from Oracle’s Official Website