Syntax sugar (syntactic sugar) in Java)

Source: Internet
Author: User

Syntaxes sugar (syntactic sugar) is a term invented by Peter J. Landin, a British computer scientist. It refers to adding a certain syntax to a computer language. This syntax makes it easier for programmers to develop programs using languages, and enhances the readability of program code to avoid errors; however, this syntax does not affect the functions of the language.
In Java, generics, variable-length parameters, automatic unpacking/packing, and Conditional compilation are all described and analyzed below.

Compared with the generics in C #, Java generics can be regarded as "pseudo generics. In C #, whether in the program source code, in the compiled intermediate language, or in the runtime generic is actually exist. Java is different. Java generics only exist in the source code and are only used by the editor. The compiled bytecode file has been erased from the generic type, at the same time, the Code for forced transformation is inserted where necessary.
Generic Code:

public static void main(String[] args) {    List<String> stringList = new ArrayList<String>();    stringList.add("oliver");    System.out.println(stringList.get(0));}

After the bytecode of the above Code is decompiled:

public static void main(String args[]){    List stringList = new ArrayList();    stringList.add("oliver");    System.out.println((String)stringList.get(0));}

Automatic unpacking/Packing
Automatic unpacking/packing determines whether to perform the unpacking and packing actions according to the code syntax during compilation.
Packing process: Package the basic types with their corresponding packaging types so that the basic types have object features.
Unpacking process: In contrast to the packing process, the packaging type is converted to the basic type.
Note that the "=" Operation of the packaging type does not automatically unpack the box without the arithmetic operator, and the equals () method of the packaging type does not process data type conversion, therefore:

Integer a = 1;Integer b = 1;Long c = 1L;System.out.println(a == b);System.out.println(c.equals(a));

This type of code should be avoided as much as possible.

Loop calendar (foreach)
List<Integer> list = new ArrayList<Integer>();for(Integer num : list){    System.out.println(num);}

Foreach requires that the iterable interface be implemented for the objects that have been passed through. Therefore, foreach iteration is also implemented by calling the underlying iterator. Decompile the source code bytecode:

List list = new ArrayList();Integer num;Integer num;for (Iterator iterator = list.iterator(); iterator.hasNext(); System.out.println(num)){    num = (Integer);}

Conditional editing
Many programming languages provide Conditional compilation. # ifdef is used in C and C ++. The Java language does not provide this pre-compilation function, but Java can also implement pre-compilation.

if(true){    System.out.println("oliver");}else{    System.out.println("lee");}

After the bytecode of this Code is decompiled, there is only one statement:


In the compiler, the Code with invalid branches is eliminated, which occurs in the de-syntactic sugar stage of the compiler.
Therefore, you can use conditional statements to implement pre-compilation.Enumeration
The enumeration type is not complex. In the JVM bytecode file structure, there is no "enumeration" type.
In fact, the enumerated type of the source program will be compiled into a common class during compilation. Inheritance and reflection are all possible.
Let's look at the following enumeration class:

public enum EnumTest {    OLIVER,LEE;}

After the bytecode is decompiled:

public final class EnumTest extends Enum {private EnumTest(String s, int i) {super(s, i);}public static EnumTest[] values() {EnumTest aenumtest[];int i;EnumTest aenumtest1[];System.arraycopy(aenumtest = ENUM$VALUES, 0,aenumtest1 = new EnumTest[i = aenumtest.length], 0, i);return aenumtest1;}public static EnumTest valueOf(String s) {return (EnumTest) Enum.valueOf(EnumTest, s);}public static final EnumTest OLIVER;public static final EnumTest LEE;private static final EnumTest ENUM$VALUES[];static {OLIVER = new EnumTest("OLIVER", 0);LEE = new EnumTest("LEE", 1);ENUM$VALUES = (new EnumTest[] { OLIVER, LEE });}}

For more details, refer to the parent class enum.

Variable Length Parameter
The variable length parameter allows us to input a variable number of parameters to the method.
For this method:

public void foo(String str,Object...args){}

We can call it like this:

foo("oliver");foo("oliver",new Object());foo("oliver",new Integer(1),"sss");foo("oliver",new ArrayList(),new Object(),true,1);

The args parameter can be any number of parameters.
In fact, in the compilation phase, argS is compiled into object [] args.

public transient void foo(String s, Object aobj[]){}

In this way, the variable length parameter can be implemented.
However, the variable length parameter must be the last of the method parameters.

In addition to the syntax sugar described above, there are also internal classes, assertions, and the switch of JDK 7 supports strings, and resources are automatically disabled (defined and closed in try.
Interested parties can decompile bytecode to understand their nature.
Undoubtedly, syntactic sugar facilitates programmer development, improves development efficiency, improves syntax rigor, and reduces the chance of coding errors. Not only do we rely on syntactic sugar in our daily coding, but we also need to see the real structure of the program code behind the syntactic sugar so that we can make better use of them.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.