Chapter 19: Generic Classes Flashcards
What is a generic class?
A generic class is a class with one or more type arguments written in <>. When an instance is made, types are supplied
What is a bound type parameter?
A type parameter can be a bound type. They specify what types of arguments are allowed e.g. extends
Where can’t type parameters be used?
We cannot refer to type parameters in any static parts i.e. class variables or class methods
We cannot create instances of a type parameter not arrays with array elements of that type. Compiler has to be able to perform type checking at compile time and virtual machine has no knowledge of type parameters so cannot create instances of them
What is raw type?
A generic class is still a class and hence a type. We can create instances without supplying type arguments, which is the raw type. When we use raw types, the compile assumes best known type for each parameter but gives unchecked warnings.
Describe Integer as a box for int
Integer can be used to warp up int values as objects. It boxes it. intValue() retrieves an int
Describe autoboxing
int and Integer can be automatically boxed and unboxed by the compiler. They work seamlessly together