Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech...

21
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki [email protected] http:// wbieniec.kis.p.lodz.pl

Transcript of Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech...

Page 1: Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki wbieniec@kis.p.lodz.pl .

Lecture 4Generic programming

AdvancedJava

Programming

1

dr hab. Szymon Grabowskidr inż. Wojciech [email protected]://wbieniec.kis.p.lodz.pl

Page 2: Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki wbieniec@kis.p.lodz.pl .

2

When you take an element out of a Collection, you must cast it to the type of element that is stored

in the collection inconvenient and unsafe.

With generics, the compiler will know the object type(same for all objects stored), so that it can be checked.

Generics

The compiler does not check that your cast is the same as the collection's type, so the cast can fail at run time.

Page 3: Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki wbieniec@kis.p.lodz.pl .

3

When we declare c to be of type Collection<String>, this tells us something about the variable c that holds true wherever and whenever it is used,

Why use generics

and the compiler guarantees it (assuming the program compiles without warnings).

A cast, on the other hand, tells us something the programmer thinks is true at a single point in the code,

and the VM checks whether the programmer is right only at run time.

Page 4: Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki wbieniec@kis.p.lodz.pl .

4

Small excerpt from the definitions of the interfaces List and Iterator in package java.util:

Might seem very similar to C++ templates so far...

Understanding generics

public interface List<E> { void add(E x); Iterator<E> iterator();}

public interface Iterator<E> { E next(); boolean hasNext();}

Page 5: Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki wbieniec@kis.p.lodz.pl .

5

...but it is different than C++ templates

In C++, templates are kind of intelligent macros.

Not so in Java. There aren’t multiple copies of the code: not in source, not in binary, not on disk and not in memory.

Understanding generics

Having vector<int> and vector<double> in our program, we also have two copies of the code: a single copy for each used type (=template

parameter).

In other words, it is a compile-time mechanism.

An analogy to plain methods: parameters in methods are values of a given type; parameters of generics are types.

Page 6: Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki wbieniec@kis.p.lodz.pl .

6

...the following piece of code:List<String> l1 = new ArrayList<String>();List<Integer> l2 = new ArrayList<Integer>();System.out.println(l1.getClass() == l2.getClass());

Consequently...

A method has formal value parameters that describe the kinds of values it operates on; a generic declaration has formal type parameters.

Understanding generics

When a method is invoked, actual arguments are substituted for the formal parameters, and the method body is evaluated.

When a generic declaration is invoked, the actual type arguments are substituted for the formal type parameters.

...prints true.

Page 7: Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki wbieniec@kis.p.lodz.pl .

7

non-generic collections – lots of casts required.No compile-time checks.

• generic collections – homogeneous, no casts required,compile-time checking.

Generics in depth

Page 8: Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki wbieniec@kis.p.lodz.pl .

8

Type variable: “placeholder” for an unknown type.NOT REALLY A TYPE:

not allowed in new expressions; cannot be derived from.

Definition of generic types

Page 9: Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki wbieniec@kis.p.lodz.pl .

9

Bounds = supertype of a type variable (Comparable<K> is the supertype in the example above).

Type parameter bounds

What for? To make available non-static members of a type variable.Limitations: gives no access to constructors or static methods.

Page 10: Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki wbieniec@kis.p.lodz.pl .

10

• with concrete type arguments,• without type arguments (yes!),

• with wildcard arguments.

Concrete instantiation

Raw type (permitted for compatibility – mixing generics with old code)

Using generic types

Page 11: Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki wbieniec@kis.p.lodz.pl .

11

Wildcard instantiation

Page 12: Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki wbieniec@kis.p.lodz.pl .

12

Wildcards

Page 13: Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki wbieniec@kis.p.lodz.pl .

13

Consider a method that draws objects from a class hierarchy of shapes.

Cannot draw e.g. a list of circlesbecause List<Circle> is NOT a subtype of List<Shape>.

Bounded wildcard example

Page 14: Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki wbieniec@kis.p.lodz.pl .

14

Fix with ‘?’ ??

Use upper bound wildcard to solve the problem.

Bounded wildcard example

Page 15: Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki wbieniec@kis.p.lodz.pl .

15

Defining a generic type – case study

Page 16: Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki wbieniec@kis.p.lodz.pl .

16

Defining constructors. Naïve approach…

Page 17: Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki wbieniec@kis.p.lodz.pl .

17

Defining constructors. Quick and dirty fix...

Page 18: Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki wbieniec@kis.p.lodz.pl .

18

Same type constructor argument

Page 19: Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki wbieniec@kis.p.lodz.pl .

19

useful cases are also rejected

Note: the abstract class java.lang.Number is the superclass of classes Byte, Double, Float, Integer, Long, and Short.

Problem with the previous example

Page 20: Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki wbieniec@kis.p.lodz.pl .

20

Compatible type constructor argument

Page 21: Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki wbieniec@kis.p.lodz.pl .

21

Equivalent implementation