+ 7

Why do we always write String args[] as a parameter in the main() function in Java ?

If I am not wrong in understanding the working of String args[] , it is used to take inputs from command line...so if that is the case then why do we use Scanner or BufferedReader classes to take inputs from the user? Pls, correct me if I'm wrong.

12th Dec 2019, 10:19 AM
Smriti Kaur
Smriti Kaur - avatar
4 ответов
+ 2
You don't have to add it i'm pretty sure, but it is a convention and it is mostly used when running batch files, you can add arguments after running the jar file. It basically gives you an array of the arguments that are inputted after the name of the file.
12th Dec 2019, 10:54 AM
coddy
coddy - avatar
+ 2
Kindly learn to run your java programs using command prompt to get a better understanding.
12th Dec 2019, 11:19 AM
Avinesh
Avinesh - avatar
+ 2
String[] args stores Java command line arguments and is an array of type java.lang.String class. String is the object in Java which supports all of the primitive datatypes like int, long, float, double, byte, shot, char. You can easily parse it in any primitive datatype.
12th Jan 2020, 8:10 AM
Shivesh Ratan