Before learning the operating system encountered string[] args this problem, but did not as the focus, today, while learning Java, decided to study the problem in depth.
The code is as follows:
public class Stringtest
{
public static void Main (string[] args)
{
for (String Arg:args) {
System.out.print (arg+ "T");
}
System.out.println ();
System.out.println ("Arg1:" +args[0]);
System.out.println ("arg2:" +args[1]);
}
}
The results of the operation are as follows:
The function of string[] args can be inferred from the operation result.
1, is used to accept the parameters of the command line.
2, the parameter type is string type, in the middle with a space to distinguish the different strings.
Of course, this kind of writing is the most familiar way of writing, but also a standard of writing.
There are other different ways of writing, for example, not necessarily to write args, but also to write other character array type variable names. Others are written in string args[, which is also derived from other languages. function is exactly the same. However, it is advisable to write a string [] args, because it is easy to recognize that this is a variable of character array type. If the latter method is used, it is easy to get hooked on string args[],argt.
public static void Main (stirng[] args); The writing of this line of code is set according to the JVM. When a Java virtual machine executes a Java application, the main () method in the program is invoked, and cannot be instantiated. So the main method should be set to a public static method. The JVM also has restrictions on the return value type and no return value. In this way, we can fully understand the meaning of this code, and also better understand the Java program execution mechanism.
Finally, we recommend a new use of String...args, you can use String...args instead of string[] args, please use the code to verify it.