What Is a Generic Class?

This section describes what is a generic class, a class that supports one or more type parameters. When a specific type value is given, a generic class becomes a normal 'specific' class.

What Is a Generic Class? A generic class is a class that supports one or more type parameters. When invoked with a given type argument, a generic class becomes a parameterized class.

A generic class uses a type parameter to make it more generic so that it can be used as multiple variations, one variation for each specific type given to the type parameter.

For example, java.util.Stack<E> is a generic class. It supports a single type parameter E. java.util.Stack<E> can be viewed as multiple classes when used with E parameterized to different reference types. Each of them is a variation of java.util.Stack<E>:

When a generic class is used with a given specific type, Behaviors of its methods will change accordingly.

For example, if java.util.Stack<Integer> is used, its method "public E push(E item)" will become "public Integer push(Integer item)".

Last update: 2014.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

Generic Classes and Parameterized Types

What Is a Generic Class?

 Using a Generic Class

 Using a Generic Class - Example

 Creating a Generic Class

 Creating a Generic Class - Example

 Bounded Type Parameters

 Raw Type, Generic Type and Parameterized Type

 Parameterized Type and Subtyping

 Wildcard Parameterized Types

 Wildcard Parameterized Type Test

 Wildcard Parameterized Subtyping

 Wildcard Parameterized Subtyping Example

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Outdated Tutorials

 References

 PDF Printing Version