Search for more details......

Array in the main method in java


Why an string array in the main method?

It is for input parameters trough the command line interface.

For example you type javac abc.java to compile a java code.

javac is the program that compile java codes to byte codes.

Compiler reads parameters after that. abc.java is parameter in the above example.

Actually, how javac reads that parameter.


In this case we have to read parameters using the String array in the main method. 

This is a example for reading command line parameters and prints each line by line
  
Save the following file as Paras.java


public class Paras{//Class for test our program


        public static void main(String args[])){//Main method of our class, arg is the String array.


              for(String para:args){

                    System.out.println(para); //Prints each line using a for loop.


              }

        }


}


Compile this using javac Paras.java

Example run

java Paras abc d ef 

This test run outputs to the console 
abc
d
ef

Example usages. 
If you want to get source file path and destination file path to your file copying application.


No comments:

Post a Comment