Java

When we hear it first thing that comes into our mind is Platform Independence and Object oriented programming. So now let us see what is actually present in java programs (Structure).

package PackageName;
import Statements;
class classname
{
variable declaration
Method declaration/definition ()
public static void main(String args[])
{
}
}

Lets check what some of the statements  that we come across above mean
1.      Package: Why do we use this statement? The reason being that when we want to group say some programs related to one domain together, we put them in same package. This statement is purely optional it need not be present if you don’t want to group related programs (but who would want that?).

2.       Import: This statement allows re-usability of existing code. If you want to use any of your program functionality that you had previously written then all that you need to do is import that program file into you current program.
3.       Class: where we will declare the class the syntax is simple class followed by a space and name of your class like

            Class Student

4.       Variable: This comes next here we will declare all the instance variables (variable that will be present for each object of class) and static variables (variables which will be shared by all objects of the class).The syntax for variable declaration is

          Datatype  Variablename;

5.      method:  declaration where we declare and define various Methods (functions) that will be invoked on the object of the class. The syntax is

         Returntype functionname(argumentlist);

         Returntype specifies the type of value the function will be returning

        Functionname is the name of the function

       Argumentlist specifies the arguments that function will take for that function

6.      main(): The main function provide the flow for the program it contains all the logic regarding how the program is to behave (takes help of the Methods defined in the class).It is not necessary that all java class have to have main() function.

Now let us see what the main function declaration contains

         public static void main(String args[])

  1. public: It provide the maximum visibility that is anyone can access variable/method that are declare as public
  2. static:  A static keyword allows the main() to be called using its class name for executing the program.
  3. void:  It specifies the return type for the function. Void means the function does not return any value.
  4. main:  It is the name of the function.The function with name main will always be executed first.
  5. String args[]:The argument list of main function contains an array of String. This array provides the main function with any command line arguments that were passed when the program was executed.

The example below will show how to pass command line argument to the program

class commandline
{
public static void main(String args[]) {
if(args.length==0)
System.out.println(“No command line arguments passed”);
else
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
}
}

To compile the program put the program file commadline.java in the bin directory of your JDK then type
javac commandline.java

Once the compiling is done execute the command given below (don’t make any changes yet to it)
java commandline arg1 arg2

Then the program will execute and you will see that you have got two lines of printed text. First line will have arg1 and second line will have arg2.

Now try changing the arg1 and arg2 to something you want or you may even increase the number of arguments in that line as well.

So now what is it that is happening around here? Well when you say “java commandline arg1 arg2” the values arg1 and arg2 will get stored in the args[] array of the main() function. “arg1” will be stored in 0th index and “arg2” will be stored in 1st index of the string array. The space between the two arguments is so that they are stored in different indexes.

If you want to run the program in eclipse then you will need to set the arguments that are to be passed in the main function. To do that Right-click on you project go to Run As->Run Configurations. Now you will see a new window popped, now double click Java Application from the left tab and on right hand side select the project and main class of your project if it has not been already selected. Now in the Arguments tab on the right hand side write the arguments that you want to pass separated from each other with blank spaces.

Eg:

“A” “B”

Xcode – For MAC OS

To run the program on XCode IDE (for MAC Users) click on product->Edit Scheme… then select Run Arguments from the left tab and click on “+” button and add you argument value in the text field containing focus  and  click ok ,now try running the program.

To run the program in NetBeans Right-click on you Project the goto “properties” then in the properties window select “Run” from the categories tab and add you Arguments in the text field corresponding label “Arguments” separate two arguments by a space. Then click ok and you are ready to run the program.

So what all is happening in the program

if(args.length==0)

if check if the args[] array has any element by checking if its length if length is zero then a message will be shown saying “No command line arguments passed”

for(int i=0;i<args.length;i++)
System.out.println(args[i]);
}

else with the for loop shown above we will be printing out the elements present in the args array.

Ok so what we have seen is a simple program that makes use of command line argument passing to the program , it was not a rocket science but how to use “ String args[] ” part of main function is not known to most of the programmers, as first program( where usually we have to get familiar to the language syntax and its structure we often miss out on the “String args[] ” part) that any one does is “Hello World” where there is no use of command line argument ,and so  how to use args[] remains a mystery.