The signature of the main method can actually be written like this:
public static void main(String... args)//Method 1
It can also be run.
And, if it also exists
public static void main(String[] args)//Method 2
will report an error that a duplicate method already exists .
It can be seen that String... args and String[] args are actually the same thing for the virtual machine.
And, within the method, through... The parameters passed in can be used exactly the same as an array. They can be used in a for loop or even directly converted:
public static void main(String... args)
{
String[] ss=args;
}
But there are still differences for programmers.
1. Call
We can only call method 2 like this:
main(new String[]{});
That is, method 2 can only accept String array. Parameters.
The unfamiliar method 1 is powerful. Call it with the following parameters and accept them all:
main();
main(null );
main(null,null);
main(null,null,null);
......
< p>main("a");main("a","b");
main("a","b","c");< /p>
......
main(new String[]{});
(String... matches String*, and null can also be A special String)
2. Parameter position
The parameter used can only be the last parameter. Otherwise, who knows which implementation will be matched when you call it? Parameters?
public static void main(String[] args,int index)//Yes
public static void main(String... args,int index)//No!
3. Overloading
Assume there are the following two methods:
public static void main(String... args)//Method 1
public static void main(String a,String... args)//Method 3
From a syntactic point of view, there is no error in this overload, and eclipse does not report an error. But when called When the number of parameters used is greater than the number of parameters in front of the dot parameter in these methods, eclipse will find this error. It's a mouthful, isn't it? Hehe~ let's give an example. The above two methods, if called < /p>
main();
The compiler will recognize that this call is method 1. But if it is called
main("");
The compiler went crazy... Because a String parameter conforms to both the dots and dots of method 1 and the String+dots of method 3, the compiler does not know which method is being called.
< p>String[] parameters will not have this problem.So pay attention when overloading. If there is a parameter of the same type in front of the dot parameter... the best way seems to be to replace it. Return to the array form, or rename the method.
4. When encountering generics and outsourcing
Use an example
java. util.Arrays is a tool class. All methods are static and operate on arrays. There is a method asList(T... args), which is used to convert an array of type T into List
This is a very useful method and will do what you want in most cases.
However, you can try the following writing method
int[] is =...//Customized array, or array obtained from somewhere
List
Unfortunately, don’t Neither execution nor compilation can pass. The error probably means:
Cannot convert List
Do you understand?
Your idea is to associate each element in int[] with each point in T...,
But the compiler doesn't think so. Because int is a primitive type, not Object A subclass of. The implicit condition of generic T is that T extends Object. Therefore, the compiler will not regard each int as a T, and will not regard int[] as a T point. Although java already supports automatically converting primitive types into Encapsulated into an outsourcing class, but that is a single situation.
The array (no matter what type) is a special type, a subclass of Object, so the compiler thinks that the entire int[] corresponds to an T, the method you called is asList