Introduction to java concepts brief knowledge of Collection framework: Generic Type erasure and java framework

Source: Internet
Author: User

Introduction to java concepts brief knowledge of Collection framework: Generic Type erasure and java framework

 

Here we want to talk about the small knowledge points that need to be understood before the collection framework. This is also a superficial understanding of the individual. If you do not know what is correct, please give us some advice.
Here we must talk about java generics, because they are closely related. Let's take a look at these lines of code:

 

Class c1 = new ArrayList<String>().getClass();Class c2 = new ArrayList<Integer>().getClass();System.out.println( c1 + "==" + c2 + " is " + (c1 == c2) );


Here we mainly want to test whether these two classes are equal. According to my previous understanding, this should be not equal, but the output result of running is:

 

Class java. util. ArrayList = class java. util. ArrayList is true

 

According to the print results, c1 and c2 are both called class java. util. ArrayList, so whether the following values are equal or not is true is totally different from what I expected,
What's going on? We need to mention the type erasue in the java generic model. There are certainly many reasons for blacktech's proposal, I excerpted some of the reasons I learned.
Let's take a step-by-step look:

 

I. Maintain code version compatibility
Before Java SE 1.5, if there is no generics, the parameter "arbitrary" can be implemented by referencing the type Object ", the disadvantage of "arbitrary" is explicit forced type conversion, which requires developers to perform with predictable actual parameter types. If the forced type conversion is incorrect, the compiler may not prompt an error and the exception occurs during running. This is a security risk. Therefore, generics are introduced later. The advantage of generics is to check the type security during compilation, and all mandatory conversions are both automatic and implicit, which improves the code reuse rate. However, you must maintain code compatibility.


2. Currently, we have learned that there are two methods for the compiler to process generics. java selects the type erasure method. The two methods are as follows:
1. Code specialization. A new object code (bytecode or binary code) is generated when a generic class or generic method is instantiated ). For example, for a generic list, three target codes may need to be generated for string, integer, and float.
2. Code sharing. Only one unique target code is generated for each generic class. All instances of this generic class are mapped to this target code, perform type check and type conversion as needed.
Template in C ++ is a typical Code specialization implementation. The C ++ compiler generates an Execution code for each generic class instance. In the Execution Code, integer list and string list are two different types. This will cause code bloat ),
Another drawback of Code specialization is that it wastes space in the reference type system, because the elements in the reference type set are essentially a pointer. There is no need to generate an Execution code for each type. This is also the main reason why the Java compiler uses Code sharing to process generics.
The Java compiler creates a unique bytecode representation for each generic type through Code sharing, and maps all instances of this generic type to this unique bytecode representation. Ing multiple generic class instances to a unique bytecode representation is implemented by type erasue.

 

Let's take a look at the general idea of Type erasure. Let's take a look at what type erasure is like:
We all know that overloading means that multiple methods can be created in the class. They have the same name, but have different parameters and different definitions. The method of processing generics according to the java editor is as follows, after the above Code is compiled, it should become like this:

 

Class c1 = new ArrayList<Object>().getClass();Class c2 = new ArrayList<Object>().getClass();System.out.println( c1 + "==" + c2 + " is " + (c1 == c2) ); 

  

It can be seen that no matter what the class is, the same class is run in the virtual machine after the compiler, so the result is equal.

 

 

 

So let's summarize some strange phenomena caused by type erasure, including:

 

  • Generic classes do not have their own Class objects. For example, the test example above.
  • Static variables are shared by all instances of the generic class. For example, a class declared as MyClass <T> shares a static variable whether it is an object created through new MyClass <String> or new MyClass <Integer>.
  • Generic Type parameters cannot be used in catch statements for Java exception handling. Because exception handling is performed by JVM at runtime. Because the type information is erased, JVM cannot distinguish two exception types: MyException <String> and MyException <Integer>. For JVM, they are all of the MyException type. The catch statement corresponding to the exception cannot be executed.

 

 

 

Related Article

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: info-contact@alibabacloud.com 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.