java syntactic sugar - generics and type erasure

Reference source: "in-depth understanding of the Java Virtual Machine," Zhou Zhiming the
syntactic sugar:
also known as "sugar-coated grammar" refers to some kind of syntax to add in computer language, the syntax of the language does not affect the function, but more convenient use programmer, but the virtual machine is running Shique not support this syntax, so they will will be syntactic sugar solution during compilation, which is reduced back to simple basic grammatical structure.

In java syntactic sugar commonly used are: Generic (but not necessarily all generic achieve syntactic sugar, such as C #, but directly supported by the CLR), variable length parameters, automatic packing / unpacking.

Is a generic JDK1.5 new features, the nature of the application that the parameterized types, data types can be said that the operation is specified as a parameter, and this parameter can be used for classes, interfaces, methods.
Until no generic, java are performed by the Object class type cast to achieve generalization, because the Object class is the parent class of all types java, so this can be converted into any Object object, then here it would be easy to produce a relatively large risk.
C # and java compare the use of generic technologies:
C # and java which is the same as if in spite of similar use, but implementation is between them there is a fundamental disagreement.
Inside C # generics either (Intermediate Language, intermediate language, this time is a generic placeholder) in the program source code, compiled in IL, CLR run-in is very real, such List<int>和List<String>that two different type, generated by the system at runtime, they have their own virtual methods and types of data, such an implementation is called type expander , generics based on this method is called real generic .
However, it only exists in the source code of java and procedures, bytecode files compiled in, it has been replaced by the original native types , that is, bare type , so the runtime of Java language, ArrayList<int>and ArrayList<String>is the same class, so the generic technique became known as syntactic sugar , a generic implementation java language called type erasure , this implementation method is called pseudo-generic .
So give chestnuts chant (in view of the author did not come into contact with C #, Java therefore will write chestnuts):
This is a simple chestnuts

 public static void main(String[] args) {
        Map<String,String> map = new HashMap<String,String>();
        map.put("hello","你好");
        map.put("how are you","吃了吗");
        System.out.println(map.get("hello"));
        System.out.println(map.get("how are you"));
    }

Take a look at the code conversion file its class, it is clear that the generic type is converted to native types

 public static void main(String[] args) {
        Map<String, String> map = new HashMap();
        map.put("hello", "你好");
        map.put("how are you", "吃了吗");
        System.out.println((String)map.get("hello"));
        System.out.println((String)map.get("how are you"));
    }

Then we look at one of my own to write an example of it

public class Test<T> {
    T a;

    public T getA() {
        return a;
    }

    public void setA(T a) {
        this.a = a;
    }
    public static void main(String[] args) {
        Test<Integer> test = new Test<>();
        test.setA(12);
        System.out.println(test.getA());
        System.out.println(test.getClass());
    }
}

Had wanted to decompile plug idea of view decompile the results of it, but it showed me that this result, I was depressed, ye no erasing it, is it wrong official yet, do not worry, here only but now that the decompilation tool of intelligence only, giving you the same as the original display.
Here Insert Picture Description
Let's look at the command line tool to view javap -c
Here Insert Picture Description
, although I do not understand this bytecode instructions, but we can come to understand through the comment next to the obvious types are erased, so in accordance with the previous understanding is correct.

Of course, when we were overloaded method, there will be some problems, that is not overloaded (that is, the type of the above erase it, leading to characteristic signature of these two methods is consistent, it can not compile)
Here Insert Picture Description
under chapter: the Java syntactic sugar - automatic packing and unpacking traversal cycle

Published 19 original articles · won praise 3 · Views 3820

Guess you like

Origin blog.csdn.net/weixin_42792088/article/details/100168953
Recommended