print
Advertisment
Advertisment

Command line arguments

Command line argument is a process with the help of which we can pass arguments to a program at run time. We can pass arguments in Java only when the operating system calls the main function. This process happens after the program is compiled. Sometimes we need to write such programs which need to pass arguments at run time then we can create such program by using command line arguments. There are many such problems for which we use command line to solve.

Example 1

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

Output :

	
        javac Command_line_argument.java //compile time
	java Command_line_argument 5,10,20 //passed runtime argument
	5
	10  //print runtime argument
	20
	100
	

Example 2

		
class Command_line_argument
{
    public static void main(String[] args)
    {
       System.out.println(args[0]);
       System.out.println(args[1]);
       System.out.println(args[2]);
       System.out.println(args[3]);
    }
}
		
	

Output :

	
        javac Command_line_argument.java //compile time
        java Command_line_argument 50 60 100 200 //passed runtime argument
	50
	60 //print runtime argument
	100
	200
	

Advertisment

Advantage and disadvantage Command Line Argument

Advantage

  • In almost all cases, the command line is much faster and easier to use for an expert.but this old way.
  • The command line can be used remotely with greater ease than GUI. But GUI is good than command line.
  • We can easily provide any number of arguments using a command line argument.But you have to convert the data type.
  • the command line is easy to develop for. Accepting arguments on the command line is trivial and outputting to a text stream is similarly easy.

Disadvantage

  • We cannot do graphics and web surfing, most office applications on command line agreement.
  • Things that we require a lot of typing, for ex long file names, can be annoying without minimal cut/paste support.
  • less time complexity.
  • The command line argument concept store the same value as it is strobe in an array.

Advertisment

Properties of Command Line Arguments:

  • in this main function works to pass.
  • It is invoked when you pass parameters at runtime of your program.
  • args[argc] is a NULL pointer.
  • args[0],args[1]...holds the name of the program..

What is the need of string args[] in command line arguments:

Friends string is a class and args is a variable or object of string class. The String class is used in the main method of Java. Which we use to take any argument from the user at run time and convert it to string and store it in args. you know args is a variable and object but we can use any type of variable.Arguments that we pass to the runtime,Here we store the value in the index like an array

Advertisment
Advertisment
arrow_upward