Why the main method having an argument in java 

Why the main method having an argument which is array reference of string type:-

  • In java command line argument is a technique to which the programmer passed the command line argument at the time of execution of the java program.
  • java<class name>  this is java program 
  • These command line argument are stored in an array of string type, This array is constructed by JVM by calculating size of command line argument.
  • When ever the command line argument the programmer pass JVM always pass the array of string type for holding the command line.
                  i.e.   java<class name>  12 56 78.
Why the main method having an argument


  • As JVM always construct array of string type for holding the command line argument then the method argument must be array reference of string type because JVM assign the base address of the array to the main method of array.

Program :-

public class argument
{
  public static void main (String args[])
    {
int size = args.length;
System.out.println("\n see the command line arguments");
for(int i=0;i<size;i++)
{
    System.out.println(args[i]); 
}
    }
}

Output :-

Why the main method having an argument

Can we extract the command line argument outside the main method ?

Answer :-
                 Yes, it is possible to extract the command line argument outside the main method as the main argument is an array reference.

program :-

public class argument1
{
  public static void main(String args[])
   {
call(args);
   }
  static void call(String ss[])
    {
int size = ss.length;
System.out.println("\n see the command line argument");
for(int i=0;i<size;i++)
  {
             System.out.println(ss[i]);
}   
    }
}
Output :-
Why the main method having an argument
Why the main method having an argument in java 
for more detail follow my Instagram id - https://www.instagram.com/asitmaharana10/

Thank you.