Why is String[] args used in main method in Java. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Why is String[] args used in main method in Java.

Please explain parsing parameters between methods.

7th Dec 2019, 1:56 AM
Shashank Reddy Voorelli
Shashank Reddy Voorelli - avatar
2 Answers
+ 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.
7th Dec 2019, 2:57 AM
Avinesh
Avinesh - avatar
+ 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.
8th Dec 2019, 4:58 AM
Avinesh
Avinesh - avatar