I\'m just beginning to write programs in Java. What does the following Java code mean?
public static void main(String[] args)
What
The style dataType[] arrayRefVar
is preferred. The style dataType arrayRefVar[]
comes from the C/C++ language and was adopted in Java to accommodate C/C++ programmers.
try this:
System.getProperties().getProperty("sun.java.command",
System.getProperties().getProperty("sun.rt.javaCommand"));
I think it's pretty well covered by the answers above that String args[]
is simply an array of string arguments you can pass to your application when you run it. For completion, I might add that it's also valid to define the method parameter passed to the main
method as a variable argument (varargs) of type String:
public static void main (String... args)
In other words, the main
method must accept either a String array (String args[]
) or varargs (String... args
) as a method argument. And there is no magic with the name args
either. You might as well write arguments
or even freddiefujiwara
as shown in below e.gs.:
public static void main (String[] arguments)
public static void main (String[] freddiefujiwara)
The following answer is based my understanding & some test.
What is String[] args?
Ans- >
String[] -> As We know this is a simple String array.
args -> is the name of an array it can be anything (e.g. a, ar, argument, param, parameter) no issues with compiler & executed & I tested as well.
E.g.
1)public static void main(String[] argument)
2)public static void main(String[] parameter)
When would you use these args?
Ans->
The main function is designed very intelligently by developers. Actual thinking is very deep. Which is basically developed under consideration of C & C++ based on Command line argument but nowadays nobody uses it more.
Thing 1- User can enter any type of data from the command line can be Number or String & necessary to accept it by the compiler which datatype we should have to use? see the thing 2
Thing 2- String is the datatype which supports all of the primitive datatypes like int, long, float, double, byte, shot, char in Java. You can easily parse it in any primitive datatype.
E.g The following program is compiled & executed & I tested as well.
If input is -> 1 1
// one class needs to have a main() method
public class HelloWorld
{
// arguments are passed using the text field below this editor
public static void main(String[] parameter)
{
System.out.println(parameter[0] + parameter[1]); // Output is 11
//Comment out below code in case of String
System.out.println(Integer.parseInt(parameter[0]) + Integer.parseInt(parameter[1])); //Output is 2
System.out.println(Float.parseFloat(parameter[0]) + Float.parseFloat(parameter[1])); //Output is 2.0
System.out.println(Long.parseLong(parameter[0]) + Long.parseLong(parameter[1])); //Output is 2
System.out.println(Double.parseDouble(parameter[0]) + Double.parseDouble(parameter[1])); //Output is 2.0
}
}
When you finish your code, you will turn it into a file with the extension .java, which can be run by double clicking it, but also throughout a console (terminal on a mac, cmd.exe on windows) which lets the user do many things. One thing is they can see console messages (System.out.print or System.out.println) which they can't see if they double click. Another thing they can do is specify parameters, so normally you would use the line
java -jar MyCode.jar
after navigating to the folder of the program with
cd C:My/Code/Location
on windows or
cd My/Code/Location
on Mac (notice that mac is less clunky) to run code, but to specify parameters you would use
java -jar MyCode.jar parameter1 parameter2
These parameters stored in the args array, which you can use in your program is you want to allow the user to control special parameters such as what file to use or how much memory the program can have. If you want to know how to use an array, you could probably find a topic on this site or just google it. Note that any number of parameters can be used.