Docsity

Understanding Generic Programming & Classes in Java at UMD, College Park - Prof. Nelson Pa, Study notes of Computer Science

Partial preview of the text

Download Understanding Generic Programming & Classes in Java at UMD, College Park - Prof. Nelson Pa and more Study notes Computer Science in PDF only on Docsity!

Generics^ CMSC 132

Department of Computer ScienceUniversity of Maryland, College Park

Overview

Generic Programming Generic Classes

Generic Class

To use a generic class you mustprovide an actual type Valid types^ Class or interface Invalid types^ Primitive type (you must usewrappers)

Generic Class

You can define your own generic classes^ Define a class and follow the class namewith the type variable(s) in angle brackets.^ Use the type variable to define the type ofvariables, type of method arguments, returntype of a method, or in object allocation You can use any name for the type variable butusually uppercase short names (E, K) are used. Example: see Queue.java in code distribution