Thursday, October 27, 2016

Java Command Line Arguments

In this article we will learn about Command Line Arguments in java. This is the fundamental concept for the  java developers.As a beginner he should have commitment on the subject whatever technology he worked on, as a java developer don't leave small topic also in core java.Be prepare some basic java interview question to get more information. Now we will discuss our topic about command line arguments in java. 

Sometime you will want the pass the information into  a program when you run it. We can acheive this by using command line arguments to main() method. That means we can send command line arguments to the main() method by using
args[0] -----> first command line argument
args[1] -----> Second command line argument

-----------------------------------------
-----------------------------------------
args.length ----> no of command line arguments

It directly fallows the the program's name on the command prompt when it executed.

public static void main(String args[])

Observer, The above statement to access the command line arguments inside the java program, they are stored as Strings in String array passed to the args parameter of main() method.  The first command line argument is stored at args[0],The second command line argument is stored at args[1] and so on. All command line arguments are passed as Strings,we need to convert numeric valuess manually

Now, we will write a program to display all the command line arguments?

class CommandLine
{
public static void main(String args[])
{
for(int i=0;i<args.length;i++)
{
System.out.println("args["+i+"]:"+args[i]);
}
  



Output:

E:\>javac CommandLine.java

E:\>java CommandLine

E:\>java CommandLine hi how are you
args[0]:hi
args[1]:how
args[2]:are
args[3]:you


From JDK5 version on words we are allowed to declare a method with variable no of arguments such type of methods are called var-args method. 


We can declare var-arg method as fallows:
m1(int... i);

The above method is applicable for number of int  arguments including zero no of arguments.

Example 1:

class Test
{
public static void m1(int... i)

{
System.out.println("var-arg methods");
}
public static void main(String args[])
{
m1();//var-arg method
m1(10);//var-arg method
m1(10,20);//var-arg method
}
}

This var-arg method internally implemented by using single dimensional array. Hence we can differentiate arguments by using index.

Example 2: 

class Test
{
public static void main(String arg[])
{
sum(1);
sum(10,20);
int a;

System.out.println(a);//will arise C.E. like variable a might not have been initialized
System.out.println(a[0]);//C.E. error like array required, but not found
sum(10);
sum();
}
public static void sum(int... a)
{
int total = 0;
for (int i=0;i<a.length ;i++ )
{
total = total+a[i];
}
System.out.println("The sum is:"+total);
}
}


Output:

E:\>javac Test.java

E:\>java Test
The sum is:1
The sum is:30
The sum is:10
The sum is:0 


Remember
  • we can't take more than one var-arg parameter in var-arg method otherwise will rise compile time error.
Ex:
m1(int...i double...d)//invalid
  • You can mix general parameter with var-arg parameter then you must write var-arg parameter last parameter otherwise will rise compile time error
  Example:
m1(char ch,int...a)//valid
m1(int...a,char ch)//invalid




 

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...