Grammatical sugar (syntactic Sugar), also known as sugar-coated grammar, refers to a grammar that is added to the language of the computer, which has no effect on the function of the language itself, only to facilitate the development of programmers and improve the efficiency of development. To be blunt, grammatical sugars are a encapsulation of existing grammars.
Java as a platform-independent high-level language, of course, also contains syntactic sugars, these syntactic sugars are not supported by the virtual machine, the encoding and decoding into the bytecode phase automatically converted into a simple common syntax. In general, the syntax sugars in Java are mainly as follows:
1. Generics and type Erasure
2. Automatic box and unboxing, variable length parameters,
3. Enhanced for Loop
4. Internal class and Enum class generics and type erase
The Java language does not support generics from the outset. In the early JDK, the functionality of generics can only be achieved through the object class being all types of parent classes and coercion type conversions. The disadvantage of enforcing type conversions is to defer the compile-time issue to runtime, and the JVM does not provide us with a compile-time check.
In JDK1.5, the Java language introduces a generic mechanism. But this generic mechanism is implemented by type Erasure, where generics in Java are valid only in the program source code (the source phase provides type checking) and are automatically substituted in the compiled bytecode with coercion type conversions. That is to say, the generics mechanism in the Java language is actually a syntactic sugar, compared with C + +, C #, its generic implementation is not so elegant.
/**
* presence of generic
/public static void main (string[] args in source code {
map<string,string> Map = new hashmap& Lt String,string> ();
Map.put ("Hello", "hi");
String Hello = map.get ("Hello");
System.out.println (hello);
}
When the above source code is compiled into a class file, the generic is erased and a forced type conversion is introduced
public static void Main (string[] args) {
HashMap map = new HashMap ();//Type erase
map.put ("Hello", "hi");
string Hello = (string) map.get ("Hello");//cast
System.out.println (hello);
}
Automatic boxing and unpacking
Automatic boxing and unboxing in Java refer to the conversion of basic data types to their wrapper types.
We know that Java is an object-oriented language, and there is one word in the Java world that says, "All things are objects." But the basic data types in Java are not objects, they do not need to do a new operation, and can not invoke any method, which has a lot of inconvenience when used. So Java provides wrapper classes for these basic types, and provides automatic boxing and unboxing for ease of use. Automatic boxing and unboxing in the use of the process, is actually a syntactic sugar, internal or call the corresponding function to convert.
The following code demonstrates the automatic boxing and unboxing features
public static void Main (string[] args) {
Integer a = 1;
int b = 2;
int C = a + B;
System.out.println (c);
}
After compiling, the code is as follows
public static void Main (string[] args) {
Integer a = integer.valueof (1);//Auto Boxing
byte B = 2;
int c = a.intvalue () + b;//automatic unboxing
System.out.println (c);
}
variable length parameter
The so-called variable length parameter is the method can accept the parameter with indefinite length
Variable-length parameter characteristics are introduced in the JDK1.5, using variable length parameters have two conditions, one is the variable length of the parameter has the same type, and the second is variable length parameter must be in the last side of the method parameter list. Variable-length parameters are also syntax sugars in Java, and their internal implementations are Java arrays.
public class Varargs {public
static void print (String ... args) {for
(string str:args) {
System.out.println ( STR);
}
public static void Main (string[] args) {
print ("Hello", "World");
}
After compiling as a class file, it is clear from this that the variable-length parameter is internally implemented through an array
public class Varargs {public
Varargs () {
} public
static void print (String ... args) {
string[] var1 = arg s;
int var2 = args.length;
Enhancing the array implementation for loop for
(int var3 = 0; var3 < var2; ++var3) {
String str = var1[var3];
System.out.println (str);
}
public static void Main (string[] args) {
//variable-length parameter conversion
to array print (new string[]{"Hello", "World"});
}
Enhance for Loop
Enhanced for loops are more powerful and simpler to code than normal for loops
An object that enhances the for loop is either an array or implements the Iterable interface. This grammatical sugar is mainly used to iterate over the array or set, which cannot change the size of the collection during the cycle.
public static void Main (string[] args) {
string[] params = new string[]{"Hello", "World"};
The enhanced for loop object is array for
(String str:params) {
System.out.println (str);
}
list<string> lists = arrays.aslist ("Hello", "World");
The enhanced for loop object implements the Iterable interface for
(String str:lists) {
System.out.println (str);
}
The compiled class file is
public static void Main (string[] args) {
string[] params = new string[]{"Hello", "World"};
string[] lists = params;
int var3 = params.length;
The enhanced for degradation of the array form is the normal for
(int str = 0; str < VAR3; ++str) {
String str1 = lists[str];
System.out.println (STR1);
}
List VAR6 = arrays.aslist (new string[]{"Hello", "World"});
Iterator var7 = Var6.iterator ();
The enhanced for implementation of the Iterable interface is traversed by using the iterator interface while
(Var7.hasnext ()) {
String var8 = (String) var7.next ();
System.out.println (VAR8);
}
Inner class
An inner class is a class that is defined within a class
The inner class is introduced in the Java language because sometimes a class is only useful in another class, and we don't want it to be used in another place. The inner class is the syntactic sugar because it is just a compile-time concept, and once compiled, the compiler generates a separate class file for the inner class, named Outer$innter.class.
public class Outer {
class inner{
}
}
After compiling with Javac, two class files Outer.class and Outer$inner.class are generated, where the Outer$inner.class contents are as follows:
Class Outer$inner {
Outer$inner (Outer var1) {
this.this$0 = var1;
}
}
The inner classes are divided into four types: Members ' inner classes, local inner classes, anonymous inner classes, static inner classes, each of which has its own usage, and the enumeration type is not described here
Enumeration types are some class constants with the same attributes
The definition of a class in Java uses class, and the definition of an enumeration class uses an enum. In the Java bytecode structure, there is no enumeration type, and the enumeration type is just a syntactic sugar that is compiled into a normal class after the compilation is complete. This class inherits Java.lang.Enum and is decorated with the final keyword.
public enum Fruit {
Apple,oringe
}
Use Jad to decompile the compiled class file to get:
//inherits Java.lang.Enum and declares final public final class Fruit extends Enum {public static fruit[] VA
Lues () {return (fruit[]) $VALUES. Clone ();
public static Fruit valueof (String s) {return (Fruit) enum.valueof (Fruit, s);
Private Fruit (String s, int i) {super (S, i);
}//enum type constant public static final Fruit APPLE;
public static final Fruit ORANGE;
private static final Fruit $VALUES [];//using arrays to maintain static {Apple = new Fruit ("Apple", 0);
Orange = new Fruit ("Orange", 1);
$VALUES = (new fruit[] {APPLE, ORANGE}); }
}