args is a special parameter array of the main method in Java, used to obtain a string array of command line parameters or external input. By accessing the args array, the program can read these arguments and process them as needed.
args in Java
What are args?
args is a special parameter of the main method in Java, which represents a command line parameter or an array of strings input from the outside.
Purpose of args
The args array stores the arguments passed to a Java program on the command line. It is mainly used to obtain user input from the command line or pass other information to the program.
Structure of args
The args array is a string array, where each element corresponds to a command line argument. The first argument is stored in args[0], and so on.
Accessing args
To access the args array, you need to specify it in the signature of the main method. A typical signature for the main method looks like this:
<code class="java">public static void main(String[] args)</code>
Example
The following example demonstrates how to get arguments from the command line and access them using the args array:
<code class="java">public static void main(String[] args) { if (args.length > 0) { // 获取第一个命令行参数 String name = args[0]; // 打印出名称 System.out.println("Hello, " + name + "!"); } }</code>
When running this program in the command line, you can pass a name as an argument and the program will print out the output with a greeting.
Note
The above is the detailed content of What does args mean in java. For more information, please follow other related articles on the PHP Chinese website!