Anonymous

Why Do We Use (String[] Arg) In Main Function In Java?

3

3 Answers

Todd Azbill Profile
Todd Azbill answered
This is where java sticks the array of arguments from the command line (if there is one)...

Example... If you have a program and to invoke it you call the following:

Java helloWorld -s -h -f:c:\test.txt

Arg would contain the following values:
Arg[0] = "-s"
Arg[1] = "-h"
Arg[2] = "-f:c:\test.txt"

This is primarily used for command-line applications.
Nilesh Tailor Profile
Nilesh Tailor answered
If you run java from command line then you pass in arguments with the program that you can use later on in the code. Arguments in this case are just values that you want passed into the code.

Example:
Student.java John Taylor
//in this case arg[0] = John arg[1] = Taylor

public static void main(String[] args){
  //the following will print to the screen John Taylor
  System.out.println(arg[0] + " " + arg[1]);
}
karthik meher Profile
karthik meher answered
Because it helps in accepting values for variables unless you want to use data input output functions(which is not that simple).

Answer Question

Anonymous