So my quick question to everyone is, would anyone be able to explain to me the basic reason as to why should I parse a argument in the (String[]args)? I'm not very knowledgeable about this topic and would like to get some quick help so I could understand things better.
Thank you!
java.exe
are exposed to your code - ulmangt 2012-04-05 00:29
You're asking why you would do it? Well, when there's a need (or requirement) to do it ofcourse. If your application should, for example, support arguments like -install
, -debug
or -foo -bar -quick -user=simpson -pass=bart
Similar to the way ls
in linux supports arguments like -R
for recursive and dir
for "Dos" supports /S
for recursive. You want to 'parse' these arguments (e.g. "take a look at them, figure out what they mean") to make your application act accordingly.
args
string[] is passed into main for a reason. When you (e.g. your code) chooses to ignore the args how is the application going to be influenced by these arguments? It's not like there's some magic thing that knows what a random string like '/FOO'
or '-WhoopteeDoo'
is supposed to do and makes your application act accordingly. It's like creating a sum(a,b)
function with a body of return 7
. By ignoring the arguments the code is not going to do what you want it to do. You'll need to return a+b
. Same goes for args[] passed into main - RobIII 2012-04-05 21:32
This is a very convenient way to work with command line applications in java. For instance, if you wanted to create a batch file to run some Java application (maybe it sets up a mail profile on your computer), you can then use the command line arguments to input even standard OS values like the name of the user, etc.
Do you need to do this all the time? Heck no, but just like everything in programming, it's a tool for us to use when it is appropriate. :)
A typical program to use command line arguments would, for example, test, if a number is prime:
java mytools.IsPrime 1234567
or a translation program could help to find a translation:
java mytools.Translate Kindergarten
On Windows, you might rarely use the commandline, but if you pass a list of files by drag and drop to your program, the OS translates this action to a list of names, which are passed to your program, so you can operate on them:
java mytools.PrettyPrint PrettyPrint.java Translate.java IsPrime.java