"Simple Java" Java type erase mechanism

Source: Internet
Author: User

Java generics are a feature introduced in JDK 5 that allows us to define classes and interfaces using parameter types, and generics are used extensively in the collection framework. Type erasure is the most confusing part of generics, and this article explains what type erasure is and how to use it.

A common error
 PackageSimplejava;Importjava.util.ArrayList; Public classQ29 { Public Static voidMain (string[] args) {ArrayList<String> Al =NewArraylist<string>(); Al.add (A); Al.add ("B");    Accept (AL); }     Public Static voidAccept (arraylist<object>al) {         for(Object o:al) System.out.println (o); }}

The above code seems to be fine because string is a subclass of object. However, this does not work, the compilation does not pass, and the following error is indicated:
The method Accept (arraylist<object>) in the type Q29 are not applicable for the arguments (arraylist<string>)

List<object> and List<string>

The reason for this is type erasure. Remember: Java's generic mechanism is implemented at the compile level. The compiler-generated bytecode does not contain generic type information during run time.

After compilation,list<object> and List<string> will become list,object and String type information is not visible to the JVM. During the compilation phase, the compiler found that they were inconsistent, so a compilation error was given.

Wildcard and bounded wildcard characters

list<? > indicates that the list can contain any type of element

     Public Static voidMain (String args[]) {ArrayList<Object> Al =NewArraylist<object>(); Al.add ("ABC");    Test (AL); }     Public Static voidTest (arraylist<?>al) {         for(Object E:al) {//no matter what type, it would be ObjectSystem.out.println (e); //in this method, because we don ' t know what type? is, we can not//add anything to al.        }    }

Always keep in mind that generics are a compile-time concept. In this case, because we don't know? , we cannot add any elements to the Al collection. You can use wildcard characters if you want to add them.

list< Object >- list can contain object or it ' s subtypelistextends number >- list can con Tain number or its subtypes. ListSuper number >-list can contain number or its supertypes.
Compare to Array

Now, we know that ArrayList <string > is not a subtype of ArrayList <object >, but you need to know that if two generic types have the same parameters, their inheritance is based on their type. such as Arraylist<string> is a subtype of collecton<string>.

However, arrays are different, they know the type of each element during run time and force their element to be that type, which is called reification. For example, object[] Objarray is a super type of string[] Strarr. If you try to add a string object to an array that stores an integral type, you will get a Arraystoreexception exception during run time.

Link: http://www.programcreek.com/2011/12/java-type-erasure-mechanism-example/

"Simple Java" Java type erase mechanism

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.