public class Test {
public static void main(String args[]) {
System.out.println("Hello World!");
}
}
When we are trying to execute our class as java Test, JVM will start the main method execution.
Here, note from the above program, main method is defined to accept String array. But, we are not passing any arguments while executing our program.
So, how JVM executing our program?
Answer is simple, when your executing the above program as java Test, JVM will call main method by passing an empty String array. It is an empty String array, but, not null. You can verify this, by printing the length of that array.
It will give you 0.
System.out.println("args length: " + args.length);//here 0You can execute main method by passing command line arguments as below.
public class Test {
public static void main(String args[]) {
for(int index = 0; index < args.length; index++) {
System.out.println(args[index]);
}
}
}
C:\>java Test First Second Third
Out Put:
First
Second
Third
No comments:
Post a Comment