+ 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?

27th Mar 2017, 7:24 PM
Rajan Kumar Shah
Rajan Kumar Shah - avatar
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]); } } }
28th Mar 2017, 3:10 AM
Heng Jun Xi
Heng Jun Xi - avatar
But we are not passing anything in the main method so why we use this parameter.
27th Mar 2017, 7:35 PM
Rajan Kumar Shah
Rajan Kumar Shah - avatar
- 2
This was in the Bible.
27th Mar 2017, 7:25 PM
Alex Snaidars
Alex Snaidars - avatar