Java Generic Erase

Source: Internet
Author: User

java Generic erase :

What is a generic wipe?

First look at what generics are. My personal understanding: Because the collection can store arbitrary types of objects. However, once the object type stored in the collection is determined, it is not possible to store other types of objects, otherwise the compilation will not error. However, classcastexception exceptions are thrown when executed. To solve this problem, introduce generics, specifying the type of objects stored in the collection object, so that once other object types are stored in the collection, the compilation will prompt for errors! (Improper understanding of the place.) Hope the Great God more guidance).

The erasure of generics means. The object type specified in the collection at compile time, but the object type that can be stored in the collection is erased at execution time!

For example, it is:

[HTML]View Plaincopy
  1. Import java.awt.List;
  2. Import java.util.ArrayList;
  3. public class Genericdemo {
  4. public static void Main (string[] args) {
  5. ArrayList<String> a = NewArrayList<String>();
  6. Only objects of type string can be stored in collection a
  7. A.add ("abc");
  8. A.add (6); Error. Stare out while performing
  9. ArrayList<Integer> b = NewArrayList<Integer>();
  10. Only objects of type integer can be stored in collection B
  11. B.add (6);
  12. B.add ("abc"); Error. Stare out while performing
  13. System.out.println (a.getclass () = = B.getclass ());
  14. Output is True
  15. /*
  16. * Cause: Generic execution with erase function; ArrayList<String>and ArrayList<Integer>Erase
  17. * After the type is ArrayList
  18. */
  19. }
  20. }
each class has a class attribute that, at the same time, demonstrates that using generics does not change the return value of the Class property.

Note: Java cannot create generic arrays: Since arrays in Java are created dynamically by reflection, generics erase type information at execution time, so there is no type information. Of course, it is not possible to construct an array.

What is wrong with the above words or the understanding of the problem, I hope to pass the great God generous enlighten!

Java Generic Erase

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.