Generics

Table of Contents

Generics

An essential characteristic of collection ADTs is that we should be able to use them for any type of data. A specific Java mechanism known as generics, also known as parameterized types, enables this capability. The impact of generics on the program- ming language is sufficiently deep that they are not found in many languages (including early versions of Java), but our use of them in the present context involves just a small bit of extra Java syntax and is easy to understand. The notation <Item> after the class name in each of our APIs defines the name Item as a type parameter, a symbolic place- holder for some concrete type to be used by the client. You can read Stack<Item> as stack of items. When implementing Stack , we do not know the concrete type of Item , but a client can use our stack for any type of data, including one defined long after we develop our implementation. The client code provides a concrete type when the stack is created: we can replace Item with the name of any reference data type (consistently, everywhere it appears). This provides exactly the capability that we need. For example, you can write code such as

Stack<String> stack = new Stack<String>();
stack.push("Test");
...
String next = stack.pop();

to use a stack for String objects and code such as

Queue<Date> queue = new Queue<Date>();
queue.enqueue(new Date(12, 31, 1999));
...
Date next = queue.dequeue();

to use a queue for Date objects. If you try to add a Date (or data of any other type than String ) to stack or a String (or data of any other type than Date ) to queue , you will get a compile-time error. Without generics, we would have to define (and implement) different APIs for each type of data we might need to collect; with generics, we can use one API (and one implementation) for all types of data, even types that are implemented in the future. As you will soon see, generic types lead to clear client code that is easy to understand and debug, so we use them throughout this book.

Generic

  1. provides compile-time (static) type safety for collections and eliminates the need for most typecasts (type conversion)
  2. A class, interface, or method that declares one or more type variables. These type variables are known as type parameters. A generic declaration defines a set of parameterized types, one for each possible invocation of the type parameter section. At runtime, all of these parameterized types share the same class, interface, or method.
  3. When you take an element out of a Collection, you must cast it to the type of element that is stored in the collection.
  4. Besides being inconvenient, this is unsafe. The compiler does not check that your cast is the same as the collection’s type, so the cast can fail at run time.
  5. Generics provides a way for you to communicate the type of a collection to the compiler, so that it can be checked. Once the compiler knows the element type of the collection, the compiler can check that you have used the collection consistently and can insert the correct casts on values being taken out of the collection.

TODO

https://www.baeldung.com/java-generics-interview-questions

Tags

  1. Autoboxing

Links to this note