This seems to be always included in java's main method, but I still don't understand what it means honestly.
Edit: thanks!
(String ...) is an array of parameters of type String, where as String[] is a single parameter.
Now here String[] can full fill the same purpose here but (String ...) provides more readability and easiness to use.
It also provides an option that we can pass multiple array of String rather than a single one using String[].
A feature of String[] vs String... is that the "String..." does not need to be part of the call.
public void test(String... args){
if(args.length > 0){
for( String text : args){
System.out.println(text);
}
}else{
System.out.println("No args defined.");
}
}
public void callerTest(){
test();
System.out.println();
test("tree");
System.out.println();
test("dog", "cat", "pigeon");
}
Then if you call callerTest(); the Output will be:
No args defined.
tree
dog
cat
pigeon
java - What is the "String[] args" parameter in the main method? - Stack Overflow
Difference between (String args[]) and (String[] args) in java
Why is String[] args required in Java? - Stack Overflow
What is the parameter in the main(String[] args) anyway?
Videos
In Java args contains the supplied command-line arguments as an array of String objects.
In other words, if you run your program in your terminal as :
C:/ java MyProgram one two
then args will contain ["one", "two"].
If you wanted to output the contents of args, you can just loop through them like this...
public class ArgumentExample {
public static void main(String[] args) {
for(int i = 0; i < args.length; i++) {
System.out.println(args[i]);
}
}
}
The program will print in the terminal:
C:/ java MyProgram one two
one
two
C:/
Those are for command-line arguments in Java.
In other words, if you run
java MyProgram one two
Then args contains:
[ "one", "two" ]
public static void main(String [] args) {
String one = args[0]; //=="one"
String two = args[1]; //=="two"
}
The reason for this is to configure your application to run a particular way or provide it with some piece of information it needs.
If you are new to Java, I highly recommend reading through the official Oracle's Java Tutorials.
i have seen that both are used in java in the main. what is the difference?
It's a String because the command line is expressed in text. If you want to convert that text into integers or booleans, you have to do that yourself - how would the operating system or Java bootstrapper know exactly how you wanted everything to be parsed? I suppose Java could look for a main method with the same number of parameters as command line arguments, and then try to parse them using Integer.parseInt etc... but this would be pretty complicated, and would still be inadequate in many situations.
As for it being mandatory - basically the Java designers decided to stick to a single method signature, which is frankly simpler than allowing for it to be optional. It would be possible though - in C#, you can have any of
static void Main()
static void Main(string[] args)
static int Main()
static int Main(string[] args)
Ultimately it doesn't make a lot of difference, to be honest. There's no significant downside in having to include the parameter.
The Java runtime system looks specifically for a method with a single String[] type parameter, because it wants to pass the parameters to your main method. If such a method is not present, it informs you through an exception.
If you want to treat the (string) command line parameters as integers or booleans, you are expected to do the conversion yourself. This allows you to handle the condition where somebody enters "ponies" where you expect an integer.