Java - Command Line Arguments / Chapter - 8 /Language Fundamental

javabychetan.blogspot.com


1.The arguments which are passing from command-prompt are called command-line arguments.
  With these command-line arguments, JVM will create an array & by passing that array as arguments
  JVM will call main( ) method.
for  example:-
class Test
{

      public static void main(String [ ] args)
     {

       System.out.println("javabychetan.blogspot.com ");
       for(int i=0;i<args.length;i++)
           {
           System.out.println(args[i]);
           }
       }
}


                                
javabychetan.blogspot.com
         















The output of the program is:

javabychetan.blogspot.com
A
B
C
Runtime-Exception ArrayIndexOutOfBoundException


Why Runtime-Exception ??
Because at args[0]=A,args[1]=B,args[2]=c,
But at  args[3] is nothing that's why we are getting Runtime-Exception ArrayIndexOutOfBoundException.

Note:-If we replace <= with < then we won't get any Runtime-Exception. 
   

2. The main advantage of  command line arguments is we can customize the behavior of the main( ) method.
    The String is taken as an argument type because:-
  • From String to any other type we can able to convert.
  • String is the most commonly used object in java.
example 2:-

class Test
{
      public static void main(String [ ] args)
     {
         String [ ] argh={"A","B","C"};
          args=argh;
          for(String s:args)
          {
                System.out.println(s);
          }
      }
}
Now

javac     Test.java
java   X  Y  Z

Output is :-

A
B
C












After
args=argh;


Next:Operators & Assignments
Back:Language Fundamental

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com