+ 5
Why is String[] args used in main method in Java.
Please explain parsing parameters between methods.
3 Respostas
+ 1
You should know what are command line arguments in order to know the purpose of using String[] args.
Consider this example and you need to use command prompt-
javac test.java // compiling
java test a b c // execution
You can see that a b c are command line arguments here. Now args [] has the values a b c.
+ 1
On command prompt you compile a java file by writing-
javac class_name.java
And you execute it by writing-
java class_name
Any thing that follows the above line are called arguments that are stored in String[] args.
For eg if I have a class test, then-
javac test.java
java test hi hello // here hi and hello are arguments passed to args[]
In the program you can print it using
System.out.println(args[0]+" "+args[1]);
This will print hi hello.