Command Line Arguments In Java With Examples | Tutorials
Beginners Programs :

Command Line Arguments In Java With Examples | Tutorials

Command line arguments is a methodology which user will give inputs through the console using commands. Whatever the concept that you preferred to learn in java , we are highly recommended to go through the examples. In reality , theoretical carries a just 20% of the subject , practically carries a lot more than 80%. Fewer technical words ! More Examples !

Table Of Contents # 1 :

Sr .NoBasic Command Line Arguments
1Definition : What is command line argument ?
2Examples : Taking Command Line Argument In Java ( 4 Unique Examples )
3 About Parse Method ( If string is a number)
4 Definition , Syntax , Different Formats
5Examples For " parse Method "
6Taking inputs with the help of parse for conversion

Command Line Arguments – Def, Syntax, Examples

In fact command line arguments are using just for a subject purpose and may be it is used in advance java core system in order to build the applications , just an expectation. Since , there may be a lot of ways to execute the program.

But , it is necessary that you need to know as a newbie about command line arguments.What are command line arguments?

The console is an interface between the user and program.

When a user enters the inputs on the console using commands, we sending the input as an argument to the main method in java that’s why in public static void main() we creating a string array to store values which work at executing time.

> In simple words , it’s just an another form of method that was used back in 2000’s year where scanner function was not introduced.

After all that a new JDK was updated further by a scanner function was implemented in order to solve the problems much more confidently.

The main difference here it has when passing the command line inputs as arguments. The inputs were directly written with the execution file command.

Thereby the function-calls the value directly from the main method ( string args[]). For instance , command line arguments are executed and compiles as follows :

  • Compile method : Javac filename.java
  • Execution method : java class name arguments

 

Example Code : 1 ( # Without command line arguments # Check output )

Output #1 :

Example code # 2 : With command line arguments

Output # 2 :

// Here Command line Arguments are 10,20 and these values are stored as an array and those values were sent to the main function. In order to get that values, we are using the method called ” string args[] ” as a formal argument. 

Example code# 3 : Another Example same as like # Example # 2 :

Output #3 :

Example code # 4 : (Sample Error Program )

Output : 

So you get an idea how to take command line arguments in java.

What Is Parse Method ?

# Syntax for a command line arguments for the conversion of a string into a number( 0,1,2,3,…N) ” Parse Method “.From the following example, you will likely to learn how to pass the command line arguments as inputs with examples. Have a look.

Note : We won’t use many technical words in this article either or any other articles on our site. We just make sure to understand the outline of the specific topic in a simple way. That’s our main motto # With examples #

# PARSE #

PARSE : It is a method which take a string(input) as an argument and convert in other formats as like :

  • Integer
  • Float
  • Double

THE TYPES OF PARSE METHODS :

  • parseInt();
  • parseDouble();
  • parseFloat();

THE FUNCTIONS OF PARSE METHODS :

  • ParseInt(); – It is a member function in Integer Class;
  • ParseDouble(); – It is a member function in Double Class;
  • ParseFloat(); – It is a member function in Float Class;

 

HOW TO PASS COMMAND LINE ARGUMENTS IN JAVA WITH EXAMPLES :

Every parse method has the same syntax but the difference is changes when the data variables changes and methodology. The conversion of a given string input can convert into different formats , as a programmer it’s all up to you which one to use according to program Int, Double , Float ( Double and Float are equal )

>> Example : ( Parse Int )

      Syntax:Integer.parseInt(String s);

Example1: String s=”156″; //156 is not a number it is string;
System.out.println(s+1); //output:1561 :”156″+1=1561 string concatenation.

int x=Integer.parseInt(“156”);
System.out.println(x+1); //output:157 :156+1=157

>> Example : ( Parse Double )

      Syntax:Double.parseDouble(String s);

Example : 2

String s=”156.5″; //156.5 is not a number it is a string;
System.out.println(s+1); //output:1561 :”156.5″+1=156.51 string concetation

double x=Double.parseDouble(“156.5”);
System.out.println(x+1); //output:157.5:156.5+1=157.5

// Double Function is as same as Float // The main difference is , where double is 8-Bytes and float is 4- Bytes. It’s all up to you which one to choose.

 Below are the examples in order to convert the given string (integer) as an integer , or double , float.

 // Conversion of String into Integer Without Parse Method // Check Out The Output //

Example program# 1 : ( Without Parse

Output : 

Error : When the user is given a number , the system won’t understand that it was a number instead of, it considers it has a string. If we suppose to add anything to that it become concatenation ex :args[0]+1=”10″+1=101

In order to get rid of that error , we are using a method called parse .Check out how the program works with the help of parse method :

Example program # 2:  ( With Parse ) : 

Output : 

// That’s How the Parse method works //.

If you have any doubts related to command line arguments , do comment here. We will help you out.

Sr.NoComplete Guide For Beginners
1.What Is Java Hashmap/Definition
2.What Are Different Notations?
3.List Of Different Hashmap Class Methods
4.Important Points To Note
5.Comparison With Similar Collections
6.Package Information

  • THU YEIN KO KO

    assume that I add two integers as command line arguments like (java example 12 34). how to store these two number in integer array and when one number is stored, the other number don’t allow, how? Please guide me