array initialization
Written at the beginning": Refactoring is rewriting code to make it more readable, easier to understand, and therefore more maintainable. The biggest investment in software is in the maintenance of the code, so sharpening knives doesn't fail to cut firewood.
An array is just a sequence of objects or primitive types of data of the same type encapsulated together with an identifier name.
There are three ways to initialize an array
The first
Integer[] a = new Integer[20];
It's still just an array of references, and the initialization process doesn't end until you create a new Integer object and assign the object to the reference. If it is a basic type, it will be automatically initialized to a null value.
In addition, when printing an array, you can use the array utility class import java.util.Arrays; Arrays.toString() method promises, or rewrite the toString () method yourself.
the second
Integer[] a = {new Integer(2),new Integer(3)};
the third
Integer[] a = new Integer[]{new Integer(2),new Integer(3),};
The last comma in the latter two initializer lists is optional, this feature makes it easier to maintain long lists
While the first form is useful, it's also more limited because it can only be used where the array is defined, and you can use the second and third forms anywhere, even inside method calls.
@EnableEurekaServer@SpringBootApplicationpublic class EurekaApplication {public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args);Other.main(new String[]{"fiddle","de ","dum"});}}class Other {public static void main(String[] args) {for(String s:args) System.out.print(s+" ");
}}
result
fiddle de dum
It can be seen from this example that public is a function of the main program, and has the same name as the program, and there can only be one
Public . Other is just a normal function
The array created for the arguments to Other.main () is created at the method call.
variable parameter list
public class EurekaApplication {public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args);Other.main("fiddle","de ","dum");Other.main("one","two");Other.main("1","2");Other.main();}}class Other {public static void
main(Object... args) {for(Object s:args){ System.out.print(s+" ");} System.out.println();}}
result
fiddle de dum
one two
1 2
With variadic parameters, the explicit syntax for writing arrays is eliminated. When you specify parameters, the compiler actually fills the array for you, and you still get an array, which is why Print () can use foreach to iterate over the array. When yes this is not just an automatic conversion from a list of elements to an array. The last line of the program shows that passing 0 arguments to the variadic argument list works.
The case where a variadic list becomes an array, and if there are no elements in the list, the size of the converted data is 0.
System.out.println(new Integer[]{1}.getClass());System.out.println(new int[]{1}.length);System.out.println(new int[0].getClass());
class [Ljava.lang.Integer;
1
class [I
System.out.println(new Integer(2).getClass());
class java.lang.Integer
The getClass () method is part of Object , he will produce the class of the object, and when printing the class, you can see the encoded string representing the type of the class. A leading " [ " indicates that this is an array of the type that follows, and an " I " that follows indicates the primitive type int . Using variadic parameter lists does not rely on the automatic wrapping mechanism, but actually uses primitive types. Types can be mixed together in a single parameter list, and the automatic wrapping mechanism will selectively promote int parameters to Integer .
Variable parameter lists complicate the overloading process.
So you can avoid the error by adding a non-variadic parameter to the overloaded method.