
- Generics - Home
- Generics - Overview
- Generics - Environment Setup
- Examples - Generic Classes
- Generics - Generic Classes
- Type Parameter Naming Conventions
- Generics - Type inference
- Generics - Generic Methods
- Generics - Multiple Type Parameters
- Generics - Parameterized Types
- Generics - Raw Types
- Examples - Bounded Type Parameters
- Bounded Type Parameters
- Generics - Multiple Bounds
- Examples - Collections
- Generics - Generic List
- Generics - Generic Set
- Generics - Generic Map
- Examples - Wild Cards
- Upper Bounded Wildcards
- Generics - Unbounded Wildcards
- Lower Bounded Wildcards
- Generics - Guidelines for Wildcards
- Type Erasure
- Generics - Generic Types Erasure
- Generics - Bound Types Erasure
- Unbounded Types Erasure
- Generics - Generic Methods Erasure
- Restrictions on Generics
- Generics - No Primitive Types
- Generics - No Instance
- Generics - No Static field
- Generics - No Cast
- Generics - No instanceOf
- Generics - No Array
- Generics - No Exception
- Generics - No Overload
- Generics Useful Resources
- Generics - Quick Guide
- Generics - Useful Resources
- Generics - Discussion
Java Generics - No Array
Arrays of parameterized types are not allowed.
//Cannot create a generic array of Box<Integer> Box<Integer>[] arrayOfLists = new Box<Integer>[2];
Because compiler uses type erasure, the type parameter is replaced with Object and user can add any type of object to the array. And at runtime, code will not able to throw ArrayStoreException.
// compiler error, but if it is allowed Object[] stringBoxes = new Box<String>[]; // OK stringBoxes[0] = new Box<String>(); // An ArrayStoreException should be thrown, //but the runtime can't detect it. stringBoxes[1] = new Box<Integer>();
Advertisements