The main method allows us to specify the code that we want to execute when we launch our application, in this way the class where it is located will be the main class or entry point of our application. For example, consider the following Main class that contains the main method:
public class Main { public static void main(String[] args) { System.out.println("Hola Mundo!"); } }
In recent versions of Java we can run this class from the terminal with the command java Main.java and we will see that the message Hello World! is printed in the console. (or from the IDE itself). The command we execute is made up of two parts, the first is java which is the command that is responsible for executing the Java code and the second is Main.java which is the name of the class that contains the main method, at this point we are using the concept of passing arguments to something, in this case the name of the class we want to execute.
Note: The - sign followed by a letter or word is commonly used to indicate that it is an argument, for example -version to obtain the version of Java that we have installed on our system. Each program can have its own plots and meanings.
java -version
Understanding this, the purpose of String[] args is to allow us to pass arguments to our application when we run it, so any arguments we place after the class name will be stored in the args array and we will be able to access them from the main method. In this way, if we execute java Main.java, everything we put after Main.java (class name) and separated by a space will be considered an argument and stored in the args array.
Let's start by running our application without passing any arguments and printing the args array through the console with the help of the Arrays.toString method:
import java.util.Arrays; public class Main { public static void main(String[] args) { System.out.println(Arrays.toString(args)); } }
java Main.java # Salida []
We get an empty array, since we are not passing any arguments, but if we execute the following command we get a different result:
java Main.java hola mundo # Salida [hola, mundo]
We are passing two arguments hello and world and these are handled as args in the main method, so we can access and perform operations with them, we can pass as many arguments as necessary. In the end the purpose of String[] args is to store all the arguments that we pass when running our application and allow us to access them from the main method. The most complicated part is processing all the arguments that are received, for example, to make a command line application we have to process, validate and convert the arguments so that they are useful in our application.
We want to make a small application that receives 3 parameters via console in the following order:
From these parameters we will perform the corresponding operation and show the result in the console. To do this, first we validate that the 3 arguments are passed, we convert the arguments to the corresponding data types, in this case the first and third arguments to the int type (numbers), and since the arguments are text strings it is not necessary to convert the arithmetic operator. Subsequently, we perform the corresponding operation and display the result in the console.
public class Main { public static void main(String[] args) { System.out.println("Hola Mundo!"); } }
Now we can run our application from the terminal and pass the corresponding arguments:
java -version
import java.util.Arrays; public class Main { public static void main(String[] args) { System.out.println(Arrays.toString(args)); } }
Being able to pass arguments to a Java application is not something unique to the terminal, it can also be done from the IDE, which is how it is commonly worked, for example, if we use IntelliJ IDEA we can pass arguments from the execution configuration of the application.
In conclusion, we can note that this is a flexible mechanism to be able to pass arguments to our application and allow us to perform specific actions based on their values, although it can be complicated to handle, for these cases we can use implementations that make our work easier such as Apache Commons CLI or JCommander. If you want to see another example of how String[] args can be used in a command line application you can check the following link.
The above is the detailed content of What is the purpose of String[] args in Java's main method?. For more information, please follow other related articles on the PHP Chinese website!