In this chapter we continue on the topic of type erasure, and we will compare the 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 {}
Looking at the code above, we can see that although the definition is just an array of fruit, it can be placed into objects of the type Apple, Fuji, and so on, since the array is a reference to the holding object and can be transformed upward at run time.
2. Generic Container
General usage:
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 common use, define a list container, the generic is filled with the same type.
However, below we will look at the characteristics of the upward transformation to see if it can be like an array?
Example:
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, no other option}}class Fruit {}class Apple extends Fruit {}class Fuji extends Apple {}
Looking at the code above, we wanted to create the list just like the array, but the generic is defined as a subclass of fruit, but the compiler throws an exception. This is because generics are not fully defined, it is simply that the compiler checks for type safety and there is no concept of generics at run time. 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 at runtime, the JVM just knows to put in object, and at the time of creation is defined the Apple subclass, although the compiler knows, but in the run time, The JVM also just knows that putting in object, this time in the runtime will cause type safety problems, so when we put in what objects will cause type safety, the compiler can only give null this option, otherwise the risk of type safety will inevitably occur.
In general, because the array is fully defined, both the compiler and the runtime will be type-detected, or the array itself is holding the object, and the generic container is only in the compiler to check type safety, and in the run time is all object, only half-definition, or the generic container does not hold objects, it will lead to the above problems.
Summary: In this chapter we mainly compare arrays with generic containers and observe what the type erasure poses to the generic container.
This chapter is here, thank you.
-----------------------------------
Directory
Learn from the java-13.11 comparing arrays with generic containers, what is the problem with observing type erasure to a generic container?