Lecture 22 - Generics

Lecture 22 - Generics - Generics Introduction • Java...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Generics Introduction • Java allows you to create classes, interfaces, and methods with parameterized data types • Added to Java in 2004 with Version 5.0 • Modeled on templates found in C++ o C++ has the Standard Template Library (STL) Has many templates for containers and algorithms Generic Classes • Are defined with one or more type parameters inside angle brackets <> o Usually single uppercase letters are used o Eg: public class MyClass<T> { . . . } Note: is in the file MyClass.java and is compiled as usual o Eg: public class KeyValuePair<T1, T2> { . . . } o The type parameter(s) can be used inside the class definition o Eg: public class KeyValuePair<T1, T2> { private T1 key; private T2 value; public KeyValuePair(T1 key, T2 value) { this.key = key; this.key = value; } public void setKey(T1 newKey) { key = newKey; } public T1 getKey() { return key; } public void setValue(T2 newValue) { value = newValue; } public T2 setValue() { return value; } } 1 byte 2 short 3 int 4 double 5 float 6 char 7 long 8 o T1 and T2 each represent a formal generic type...
View Full Document

This note was uploaded on 11/04/2010 for the course CPSC 233 taught by Professor Li during the Fall '09 term at University of Calgary.

Page1 / 5

Lecture 22 - Generics - Generics Introduction • Java...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online