Learn from the java-13.11 control arrays and generic containers, what is the problem with observing type erasure to a generic container?

Source: Internet
Author: User

In this chapter we continue on the topic of type erasure, and we will use the control array with the generic container to see what problems the type erasure brings to the generic container?

1. Arrays

Package Com.ray.ch13;public class Test {public static void main (string[] args) {fruit[] fruits = new Apple[5];fruits[0] = New Apple (); fruits[1] = new Fuji (); fruits[2] = new Fruit ();}} Class Fruit {}class Apple extends Fruit {}class Fuji extends Apple {}

Observing the above code, we can see. Although the definition is only an array of fruit. But it can be put into objects such as Apple, Fuji, and so on, because the array is a reference to the owning object and can be transformed upward as it executes.


2. Generic Container

General methods of Use:

Package Com.ray.ch13;import Java.util.arraylist;public class Test {public static void main (string[] args) {arraylist< fruit> fruits = new arraylist<fruit> () Fruits.add (New Fruit ()); Fruits.add (new Apple ()); Fruits.add (new Fuji () );}} Class Fruit {}class Apple extends Fruit {}class Fuji extends Apple {}

Above is our regular use of the method. Defines a list container. Generics are filled with the same type.

However, the following will be based on the characteristics of the upward transformation to see if we can be like an array?

Examples:

Package Com.ray.ch13;import Java.util.arraylist;public class Test {public static void main (string[] args) {//ARRAYLIST&L T fruit> fruits = new arraylist<apple> ();//errorarraylist<? Extends fruit> fruits = new arraylist<apple> ();                Fruits.add (New Apple ());//error//Fruits.add (New Fruit ());//errorfruits.add (null);//This can only be null, there is no other option}}class Fruit {}class Apple extends Fruit {}class Fuji extends Apple {}

Looking at the code above, we wanted to be like an array, when we created the list, we simply defined the generic as a subclass of fruit, but. The compiler throws an exception.

This is because generics are not completely defined, it is simply the compiler checking for type safety. There is no concept of generics at the time of execution. And when we define it by the wildcard character? " "To define, that is to say I can put in the fruit itself or subclass, the compiler can know, but in the execution period. The JVM simply knows to put the object in. The Apple subclass was defined when it was created. Although the compiler knows. But in the implementation period. The JVM also simply knows that the object is put in, and this time it will cause type-safety issues in the execution period. So, when we put in something that would cause type safety, the compiler could simply give null, or the risk of type safety would be certain.

In general, because arrays are completely defined, type checking occurs both at the compiler and execution time, or the array itself is a holding object, and the generic container is simply checking for type safety in the compiler. But in the execution period is all object, is only half definition. Or, the generic container does not hold objects, leading to the problem shown above.


Summary: In this chapter we mainly control arrays and generic containers, and observe what the type erasure poses to the generic container.


This chapter is here. Thank you.

-----------------------------------

Folder






Learn from the java-13.11 control arrays and generic containers, what is the problem with observing type erasure to a generic container?

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.