review of 1323

36
Java Generics Andrew H. Fagg: CS 2334: Java Generics 1

Upload: others

Post on 01-May-2022

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Review of 1323

Java Generics

Andrew H. Fagg: CS 2334: Java Generics 1

Page 2: Review of 1323

Arrays Class

Provides, among other things, static methods for sorting primitive arrays of different types (byte, char, int, double)

Andrew H. Fagg: CS 2334: Java Generics 2

Page 3: Review of 1323

Arrays Class

Problems with this?

•Separate implementation for each type

•Each new type needs a new implementation

Solutions?

Andrew H. Fagg: CS 2334: Java Generics 3

Page 4: Review of 1323

Arrays Class

Solutions?

•Could provide a static method that sorts an array of Objects

Andrew H. Fagg: CS 2334: Java Generics 4

Page 5: Review of 1323

Arrays Class

Could provide a static method that sorts an array of Objects

•But - what does it mean to compare two arbitrary Objects so that we can establish an ordering between them?•For example a String and an Integer?

•We really need a way of talking generically about a homogeneous array of Objects

Andrew H. Fagg: CS 2334: Java Generics 5

Page 6: Review of 1323

Java Generics•A type becomes a parameter to a class and/or a method:public class ClassName<T>{

:

}

•T is the variable type that is assigned when we use the class

•Within the class definition, we can “pretend” that it is a real type (parameters, variable declarations and return types)

Andrew H. Fagg: CS 2334: Java Generics 6

Page 7: Review of 1323

GenericStack example …

Andrew H. Fagg: CS 2334: Java Generics 7

Page 8: Review of 1323

Standard Generic Type Names

Generic type symbols are arbitrary, but we tend to use a few:

•E - Element (used extensively by the Java Collections Framework)

•K - Key

•N - Number

•T - Type

•V – Value

Andrew H. Fagg: CS 2334: Java Generics 8

Page 9: Review of 1323

Advantages of Generics

•Code reuse•ArrayList, Java Collections Framework

•Specific types are checked at compile time (as opposed to everything having to be an Object)•Reduces runtime errors

•Easier to read and understand code when we can be very explicit about types

Andrew H. Fagg: CS 2334: Java Generics 9

Page 10: Review of 1323

Notes

•Primitive types cannot be used as generic types•Must use the wrapper classes

•Type erasure: generics are checked at compile time, not at runtime•This decision was made to maintain backward

compatibility•Not a serious issue most of the time

Andrew H. Fagg: CS 2334: Java Generics 10

Page 11: Review of 1323

Implications of Type Erasure

•Cannot construct objects of type EE myData = new E(); // illegal code

•Cannot construct arrays of type EE[] elements = new E[capacity]; // illegal

•Solution to the latter: create an array of objects and then cast to array of E

E[] elements = (E[]) new Object[capacity]; // Legal

Andrew H. Fagg: CS 2334: Java Generics 11

Page 12: Review of 1323

Implications of Type Erasure

• instanceof() cannot distinguish same class with different generic type, because it is done at run time•ArrayList<Integer> and ArrayList<String> are the

same type according to instanceof

•Exception classes cannot be generic

•Static data cannot be of a generic type

Andrew H. Fagg: CS 2334: Java Generics 12

Page 13: Review of 1323

Inheritance and Generics

•In many situations, we might have more than one generic type as part of a class or method definition

• These could be arbitrary types or we might want them to have some specific relationship•For example: we might want T1 to be a superclass

of T2

Andrew H. Fagg: CS 2334: Java Generics 13

Page 14: Review of 1323

Administrivia

•Lab 5 grades: coming

•Project 1 grades: posted

•Exam 1 grades & returned exams: posted and emailed

•Lab 7 coming soon

Andrew H. Fagg: CS 2334: Java Generics 14

Page 15: Review of 1323

Generics

A type becomes a parameter of another type definition

For example: GenericStack<Person>

•Code reuse

•Standard interfaces

•Type checking at compile time

•Type erasure: generic types are lost at run time

Andrew H. Fagg: CS 2334: Java Generics 15

Page 16: Review of 1323

Class Hierarchies

Andrew H. Fagg: CS 2334: Java Generics 16

Number

Integer

Object

Double

Page 17: Review of 1323

Class Hierarchies

Integer i = new Integer(42);

Number n = new Integer(1138);

Andrew H. Fagg: CS 2334: Java Generics 17

Number

Integer

Object

Double

Page 18: Review of 1323

Class Hierarchies

Andrew H. Fagg: CS 2334: Java Generics 18

Number

Integer

Object

Double

ArrayList<Number>

ArrayList<Integer>

???

Page 19: Review of 1323

Class Hierarchies

Andrew H. Fagg: CS 2334: Java Generics 19

ArrayList<Number>

ArrayList<Integer>

???

Page 20: Review of 1323

Class Hierarchies

The only common (specific) ancestor is Object…

Andrew H. Fagg: CS 2334: Java Generics 20

Object

ArrayList<Number>

ArrayList<Integer>

???

ArrayList<Number> ArrayList<Integer>

Page 21: Review of 1323

GenericTest example

Andrew H. Fagg: CS 2334: Java Generics 21

Page 22: Review of 1323

Wildcards

•We still want a way of saying that we will accept any type as input to a generic

•Or – we want to put constraints on the type

Andrew H. Fagg: CS 2334: Java Generics 23

Page 23: Review of 1323

Wildcards

There is a class hierarchy that we can use…

Andrew H. Fagg: CS 2334: Java Generics 24

ArrayList<? extends Number>

ArrayList<Integer>

ArrayList<?>

ArrayList<Double> ArrayList<Number>

Page 24: Review of 1323

Wildcards

But, there is a hierarchy that we can use…

Andrew H. Fagg: CS 2334: Java Generics 25

ArrayList<? extends Number>

ArrayList<Integer>

ArrayList<?>

ArrayList<Double> ArrayList<Number>

“ArrayList of anything”

Page 25: Review of 1323

Wildcards

But, there is a hierarchy that we can use…

Andrew H. Fagg: CS 2334: Java Generics 26

ArrayList<? extends Number>

ArrayList<Integer>

ArrayList<?>

ArrayList<Double> ArrayList<Number>

“ArrayList of anything that is a

subclass of a Number”

Page 26: Review of 1323

Wildcards

Andrew H. Fagg: CS 2334: Java Generics 27

ArrayList<? extends Number>

ArrayList<Integer>

ArrayList<?>

ArrayList<Double> ArrayList<Number>

ArrayList<Integer> list1 = new ArrayList<Integer>();

ArrayList<? extends Number> list2 = list1; // Legal

Page 27: Review of 1323

Example: sum a stack of Numbers

Andrew H. Fagg: CS 2334: Java Generics 28

Page 28: Review of 1323

Binary Search

Search for a key in an array and return it’s index

•One possible implementation:public static int <T>

binarySearch(T[] a, T key, Comparator<T> c)

•The Comparator allows us to compare the key against the elements of the array•The generic implementation doesn’t require knowledge of the specific object types

Andrew H. Fagg: CS 2334: Java Generics 29

Page 29: Review of 1323

Binary Search

Could we be more general about what Comparators are acceptable?

•Suppose T = Double

Andrew H. Fagg: CS 2334: Java Generics 30

Page 30: Review of 1323

Binary Search

Could we be more general about what Comparators are acceptable?

•Suppose T = Double

•Could a Comparator<Number> work?•Yes! Number allows access to the doubleValue

•public static int compare(Number d1, Number d2)• If(d1.doubleValue() < d2.doubleValue()) return -1;•…

Andrew H. Fagg: CS 2334: Java Generics 31

Page 31: Review of 1323

Wildcard Example I

Arrays in Java API (actual implementation):binarySearch(T[] a, T key, Comparator<? super T> c)

•The class that is passed as the third parameter must implement the Comparator interface for type T or a superclass of type T

Andrew H. Fagg: CS 2334: Java Generics 32

Page 32: Review of 1323

Wildcards

The complement…

Andrew H. Fagg: CS 2334: Java Generics 33

ArrayList<? super Number>

ArrayList<Object>

ArrayList<?>

ArrayList<Number>

Page 33: Review of 1323

Wildcards

The complement…

Andrew H. Fagg: CS 2334: Java Generics 34

ArrayList<? super Number>

ArrayList<Object>

ArrayList<?>

ArrayList<Number>

“ArrayList of anything that is a

superclass of a Number”

Page 34: Review of 1323

Wildcards

Andrew H. Fagg: CS 2334: Java Generics 35

ArrayList<? super Number>

ArrayList<Object>

ArrayList<?>

ArrayList<Number>

ArrayList<Object> list1 = new ArrayList<Object>();

ArrayList<? super Number> list2 = list1; // Legal

Page 35: Review of 1323

Wildcard Example II

Example: Copy from one GenericStack to anotherpublic static<T> void

copy (GenericStack<? super T> dest,

GenericStack<? extends T> src)

•The <T> before the method name determines the base type

•The source must be a class that is or extends T

•The destination must be a class that is or is a superclass of T

Andrew H. Fagg: CS 2334: Java Generics 37

Page 36: Review of 1323

Wildcards and Generic Types

•Give us a tremendous amount of flexibility

•Wildcard types are defined and checked at compile time•Reduce runtime errors!

•Lab 7: we will define:•Generic notion of a Card<T>•Generic notion of a Deck<T, E extends Card<T>>

Andrew H. Fagg: CS 2334: Java Generics 39