Zaawansowane programowanie obiektowe Lecture 1 Szymon Grabowski [email protected] Łódź, 2011.
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech...
-
Upload
neal-little -
Category
Documents
-
view
218 -
download
1
Transcript of Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech...
Lecture 4Generic programming
AdvancedJava
Programming
1
dr hab. Szymon Grabowskidr inż. Wojciech [email protected]://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.
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.
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();}
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.
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.
7
non-generic collections – lots of casts required.No compile-time checks.
• generic collections – homogeneous, no casts required,compile-time checking.
Generics in depth
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
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.
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
11
Wildcard instantiation
12
Wildcards
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
14
Fix with ‘?’ ??
Use upper bound wildcard to solve the problem.
Bounded wildcard example
15
Defining a generic type – case study
16
Defining constructors. Naïve approach…
17
Defining constructors. Quick and dirty fix...
18
Same type constructor argument
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
20
Compatible type constructor argument
21
Equivalent implementation