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


javabychetan.blogspot.com

java command line arguments 


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]);

           }

       }

}

Command Line Arguments in java






The output of the program is:

javabychetan.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.

  • The 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;

 


No comments:

Post a Comment

Be the first to comment!

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.

Regards,
JavaByChetan
Back To Home

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com