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:

Accepting Inputs Through Command Line Arguments In Java, command line argumnets in java


Case 2:


Case 3:


Case 4:


Case 5:


Case 6 ( Passing Integers )


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 TypeWrapper ClassRange
charCharacterNo parse method available


Addition Of Number Using Wrapper Classes



Guess the output ?


During the execution if we pass integer value as a first input and String as a second input


bin> java AddNos 10 Varanasi


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


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

Command Line Arguments In Java

Editorial Staff

Editorial Staff at TCA is a team of Programmers led by Shubham Tripathi. Trusted by over 100K readers worldwide

    See all posts by shubham