Java Syntax sugar
Syntactic sugars add some syntactic support at the language level to facilitate developer code development, and these syntaxes do not improve the performance of the program, only to improve development efficiency.
Java syntax sugar requires the support of the Java compiler, in the process of compiling the source code into bytecode, the compiler will solve the syntax of the sugar operation, the syntax sugar to revert to more common Java syntax.
Common Java syntax for sugar
- Generics and type Erasure
Generics are parameterized data types to manipulate, and Java introduces generics in JDK1.5.
Java implements generics in the form of type erasure, that is, after it is compiled into bytecode, reverted to the parent type of the parameterized type, and if no parent type is specified, reverts to the object type, which is implemented by forcing the type conversion where a particular parameterized type is required.
newnew ArrayList<Banana>();eat(apples);eat(bananas);publicvoideat(List<Apple> apples) { for (Apple apple : apples) { apple.eatApple(); }}publicvoideat(List<Banana> bananas) { for (Banana banana : bananas) { banana.eatBanana(); }}
After the type is erased, it becomes similar to the following code:
newnew ArrayList();eat(apples);eat(bananas); publicvoideat(List apples) { for (Object apple : apples) { ((Apple) apple).eatApple(); }}publicvoideat(List bananas) { for (Object banana : bananas) { ((Banana) banana).eatBanana(); }}
Because Java uses type erase to implement generics, there are some confusing results, and for the above example the two eat methods cannot exist as overloaded methods in the same class, although it looks like public void eat(List<Apple> apples) public void eat(List<Banana> bananas) two different methods, However, after the type is erased, it is the same method for the compiler as two signatures public void eat(List fruits) .
- Automatic packing and unpacking
Automatic boxing and unpacking provides an automatic conversion between some classes and their corresponding atomic types, such as:
Integer 和 int类型Long 和 long类型
The principle of automatic boxing and unpacking implementation calls the wrapper and unpacking methods of the corresponding class type, as follows:
Integer integer = 0;int value = integer;
After solving the grammatical sugars, namely:
Integer integer = Integer.valueOf(0);int value = integer.intValue();
For-each Loop is a simple way to do a For loop
for (Apple apple : apples) { apple.eatApple();}
After parsing the syntax sugar, it reverts to the way the iterator was used:
for (Iterator iterator = apples.iterator(); iterator.hasNext();) { Apple apple = (Apple) iterator.next(); apple.eatApple();}
Therefore, the interface must be implemented for classes that can use the For-each loop traversal java.util.Iterable .
- Variable length parameter
The variable-length parameter is the variable number of arguments for a method, and the syntax for variable-length parameters:
public static void main(String ..args) {}
The variable-length parameter is implemented using an array, the above method is equivalent to:
public static void main(String[] args) {}
The implementation of the Java inner class is actually a syntactic sugar:
public class OutClass { private int value; private class InnerClass { InnerClass() { System.out.println(value); } } }
The compiler generates one OutClass$InnerClass and adds a type of parameter for each of its construction methods OutClass . :
public class OutClass { private int value; static int access$000(OutClass out) { return out.value; }}class OutClass$InnerClass { private OutClass$InnerClass (OutClass out) { System.out.println(Out.access$000(out)) }}
Because the internal class object is constructed with references to external class objects, the member variables and methods of the outer class can be accessed internally. If an inner class is to be a private member in an external class, the compiler generates a special method in the outer class (the method in the preceding example access$000 ) for the inner class to access its private members.
The implementation of the Java enumeration class is also an important syntactic sugar
enum Color { Red, Green, Blue}
The above enumeration class is converted to a normal class after compilation Color , which is declared and final inherited from the java.lang.Enum class:
public final class Color extends java.lang.Enum { private Color(String name, int value) { super(name, value); } public static final Color Red = new Color("Red", 0); public static final Color Green = new Color("Green", 1); public static final Color Blue = new Color("Blue", 2);}
ColorThere are three constants defined in the class, namely,, Red Green Blue .
Reference: "In-depth understanding of Java virtual machines"
Java syntax sugar