Junior — Middle
What methods can be used to pass parameters to the main function in a program?
sobes.tech AI
Answer from AI
In Java, the main function is usually declared as public static void main(String[] args). Parameters are passed through the string array args, which contains command-line arguments.
Example of passing parameters:
public class Main {
public static void main(String[] args) {
for (String arg : args) {
System.out.println(arg);
}
}
}
Running the program with parameters:
java Main param1 param2
Parameter passing options in main are limited to a string array. There are no other standard ways to pass parameters to main, but within the program, other data transfer methods can be used (for example, through files, environment variables, system properties).