First copy a definition:
The grammatical sugar (syntactic Sugar), also known as icing grammar, is a term invented by the British computer Peter.j.landin, referring to a grammar that is added to a computer language, which has no effect on the function of the language, but is more convenient for programmers to use. The most commonly used syntactic sugars in Java are generics, variable-length parameters, conditional compilation, automatic disassembly boxes, inner classes, and so on. The virtual machine does not support these grammars, which are reverted back to the simple basic syntax structure during the compilation phase, which becomes the solution to the syntax sugar.
In short, syntax sugar is a syntactic process that is designed to facilitate programming, but does not affect the language itself. Although there is no material modification to the language itself, syntactic sugars can improve performance in programming, or can improve the rigor of grammar, or reduce the chance of coding errors.
variable length parameter : The parameter of our incoming method is not a fixed number
1 @Test2 Public voidTest_getstr () {3Getstr ("111", "222", "333");4 }5 6 Public voidgetstr (String ... ss) {7StringBuilder SB =NewStringBuilder ();8 for(inti = 0; i < ss.length; i++) {9 sb.append (Ss[i]);Ten } One System.out.println (SB); A}
The parameter of the Getstr method is an indeterminate parameter, but the array used in the Traverse parameter, the visible variable length parameter is implemented using an array. Let's look at the compiled class file:
@Test publicvoid test_getstr () { getstr (new String [] {"111", "222", "333" }); Public void getstr (String ... ss) { new StringBuilder (); for (int i = 0; i < ss.length; i++) { sb.append (ss[i]); } System.out.println (SB); }
The parameters in the test method called GETSTR (new string[] {"111", "222", "333"}) have been transformed into arrays, and the mutable parameters are implemented using arrays.
But why not use the array directly, the reason is very simple, the array initialization is required to specify the length, in the case of unknown length, the use of variable-length parameters than the array convenient, and the compilation phase according to the number of parameters converted into a specific array to the compiler to deal with, the programmer is not very comfortable.
The last important point: the variable length parameter must be the last parameter in the method parameter list and only one variable length parameter in the method parameter list
Java syntax sugar 1: variable length parameters