Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introducing Java Generics and Collections

Similar presentations


Presentation on theme: "Introducing Java Generics and Collections"— Presentation transcript:

1 Introducing Java Generics and Collections
CSC 202

2 Before Java 1.5 . . . No generics.
Collections existed (since 1.2), but the capabilities made possible by generics obviously were not available.

3 Generics? What is it (are they)?
The ability to pass types as arguments. Aside: What’s the difference between parameters and arguments? To use C++ terminology, Java parameters are similar to formal parameters which appear in the function definition, and arguments are like actual parameters which appear in the function call.

4 Popularity of “Generics”
A.K.A. Templates Parametric polymorphism Found in Ada C++ C#

5 Syntax Change due to Generics
Use of angle brackets For example LinkedList <String> Read as “LinkedList of String.” Why not parentheses? To set them apart from variable parameters.

6 The Purpose of Generics
Two words: type safety. “The behaviors classified as type errors by any given programming language are generally those that result from attempts to perform on some value (or values) an operation that is not appropriate to its type (or their types).” Wikipedia One of the first common-sense principles learned in software engineering . . . The earlier we can uncover a defect, the cheaper it is to correct it.

7 Generics Used With Collections
The generic feature in Java lets you tell the compiler about the type that you expect to load into a collection class. As an extra benefit, you no longer have to write as many casts.

8 Example of Using Generics To Enhance Type Safety
Before 1.5, collection classes (such as ArrayList) could contain Objects only. If you intended for an ArrayList to contain only Strings, nothing prevented the accidental insertion of an Integer. Discovery of the unintended Integer (or other non-String) in the ArrayList might not happen until much later, perhaps causing a run-time error. Now, however, the Integer insertion can be reported at compile time!

9 ArrayList and other Collections
TreeSet HashMap LinkedList HashSet LinkedHashMap Others . . . Each has its own characteristics that may make it uniquely suited for an application. See the Java API for details. p. 533, p. 557, and API (Java Collection Framework)

10 Elements of a Generic Class
Denoted as “E” or “T” “E” is a stand-in for the type of element you want a collection to hold and return. See ArrayList documentation in API GenericStack.java example (text, p. 688)

11 Generic Methods Use type parameter in declaration.
public <T extends Animal> void takeThing (ArrayList <T> list) T can be “any type of Animal”. Note: This is not the same as public void takeThing (ArrayList <Animal> list) The first one takes any ArrayList declared as type Animal or any subtype of Animal (e.g., Dog, Cat). The second takes only ArrayLists of Animal type. Another note: Generic types must be reference types (not primitive types).

12 Generic Methods See example listing 21.2 (text, p. 690).
public <T extends Animal> void takeThing (ArrayList <T> list) T can be “any type of Animal”. Note: This is not the same as public void takeThing (ArrayList <Animal> list) The first one takes any ArrayList declared as type Animal or any subtype of Animal (e.g., Dog, Cat). The second takes only ArrayLists of Animal type. Another note: Generic types must be reference types (not primitive types).

13 Raw Types Specification of a generic class without a type parameter.
Allowed for backward compatibility. Unsafe.

14 Confused yet? In Java generics syntax, extends really means extends or implements. The “?” (wildcard) Use any type in place of the type parameter. Example: public void sampleMethod(String arg1, ArrayList<?> arg2) sampleMethod has two arguments, the first of which must be a String. The second can be an ArrayList<T> with any base type.

15 Why use “?” ? The difference is that Equivalent examples:
T stands for any subtype, but it can be a specific subtype within the context, and ? stands for any subtype. Equivalent examples: public <T extends Animal> void takeThing (ArrayList<T> one, ArrayList<T> two) Public void takeThing (ArrayList<? extends Animal> one, ArrayList<? extends Animal> two)

16 “T” is not a magic letter
Note: you may use any nonkeyword identifier for the type parameter; you need not use T. (Savitch, Absolute Java, 2d ed., p. 782) The custom is to start the identifier with an uppercase letter; using a single letter is a weaker tradition.


Download ppt "Introducing Java Generics and Collections"

Similar presentations


Ads by Google