+ 1
Why is String arg[] used in main method of java?
In the main method of java programmeâ we write public static void main(String arg[]) why is the String arg[] used?
3 Answers
+ 5
Java program can be run by using cmd, this is where the String[] args come into play.
http://stackoverflow.com/questions/890966/what-is-string-args-parameter-in-main-method-javaIn Java args contains the supplied command-line arguments as an array of String objects.
In other words, if you run your program as java MyProgram one two then args will contain ["one", "two"].
If you wanted to output the contents of args, you can just loop through them like this...
public class ArgumentExample {
public static void main(String[] args) {
for(int i = 0; i < args.length; i++) {
System.out.println(args[i]);
}
}
}
0
But we are not passing anything in the main method so why we use this parameter.
- 2
This was in the Bible.