Syntactic sugars (syntactic Sugar): Also known as icing syntax, refers to a grammar that is added to a computer language, which has no effect on the functionality of the language, but is more convenient for programmers to use. In general, the use of syntactic sugars can increase the readability of the program and reduce the chance of error in program code.
Java, in contrast to C # and many other JVMs, is a "low-sugar Language" in modern compilation languages. Especially before JDK1.5, the "low sugar" syntax is also a manifestation of the Java language being suspected of being "backward".
In the source code of Javac, the process of solving the syntactic sugars is triggered by the Desugar () method, which is done in the Com.sun.tools.javac.comp.TransTypes class and the Com.sun.tools.javac.comp.Lower class.
Syntax sugars in Java include, but are not limited to, the following 10: Generics and type erase, auto-boxing and unboxing, traversal loops, variable-length parameters, conditional compilation, inner classes, enumeration classes, assertion statements, switch support for enumerations and strings, definition and closing of resources in a try statement.
1. The parameterized type in generics and type erase Java is only present in the source code, and in the compiled bytecode, it has been replaced with the original native type, and the cast code is inserted in the corresponding place. For the runtime Java language,,arraylist<integer> and arraylist<string> are the same class. So generics are actually a syntactic sugar in the Java language, and generic implementations in the Java language are called type Erasure, and generics based on this approach are called pseudo generics. Generics and type Erasure
public void Parameterizedtype () {
list<string> list = new arraylist<string> ();
List.add ("Fans.lei");
}
2. Automatic boxing and unpacking automatic boxing and unpacking implements an implicit conversion between the base data type and the object data type. Automatic packing and unpacking
public void Autobox () {Integer one = 1;
if (one = = 1) {
System.out.println (one);
}
}
The 3.foreach cyclic foreach statement is one of the new features of Java5, and foreach provides developers with great convenience in traversing arrays and collections.
The foreach statement is a special simplified version of the for statement, but the foreach statement does not completely replace the For statement, however, any foreach statement can be rewritten as a version of the for statement. foreach is not a keyword, and it is customary to call this special for-statement format a "foreach" statement. foreach Loop
public void foreach () {
Integer[] Array = {1, 2, 3, 4, 5};
for (Integer I:array) {
System.out.println (i);
}
}
4. Variable length parameter arrays.aslist (1, 2, 3, 4, 5); public static <T> list<t> aslist (T ... a) {
return new Arraylist<> (a);
}
5. Conditional compilation/Conditional compilation
public void ifdef () {if (true) {
System.out.println ("true");
} else {//here is a warning--deadcode
System.out.println ("false");
}
}
6. Internal class Public classes Javatensugar {class Name {
String FirstName; String Secondname;
}}
7. Enumerate class public enum SEX {
Man, WOMAN
}}
8. Assertion statement//runtime need to add VM parameter-ea public static void Main (string[] args) {
String str = "FANS2";
Boolean flag = "fans". Equals (str);
ASSERT (flag);
}//program results exception in thread "main" Java.lang.AssertionError
At Sugar. Javatensugar.main (javatensugar.java:102)
9. Switch support for enumerations and strings//switch support for enumerations and strings
public void Enumstringswitch () {
String str = "fans";
Switch (str) {
Case "fans":
Break;case "Leiwen":
Break;default:
Break
}
}
10. Define and close resources in a try statement Jdk7 provides try-with-resources that automatically shuts down related resources (as long as the resource implements the Autocloseable interface, JDK7 implements this interface for most resource objects)
Staticstringreadfirstlinefromfile (Stringpath) throwsioexception{
Try (Bufferedreaderbr=newbufferedreader (Newfilereader (path))) {
Returnbr.readline ();
}
}
10 syntax sugars in Java