0
What do the main method parameters actually mean?
So you write your main method as public static void main (String [] args){ } My question is that main is a method that has takes parameters and we never invoke main as an object we always reference objects to main. So what does String[] args actually do to the method?
2 Réponses
+ 2
The java interpreter is the thing that invokes the main method. The array of Strings parameter is the mechanism through which the runtime system passes information to your application. Each String in the array is called a command line argument.
0
If you run your java program – let's say it's a class called HelloWorld – from the command line, like this:
> java HelloWorld
then you can pass arguments into the main() method by adding them after the name of the class, separated by spaces. So, something like this:
> java HelloWorld Mike
could be passed into a main method that looked like this:
public static void main(String[] args) {
System.out.println(args[0] + " says Hello, World!");
}
for an output like this:
> Mike says Hello, world!
In a real-world coding environment, you might use this to pass in configuration options for your program, or something like that.