Download presentation
Presentation is loading. Please wait.
Published byNeal Little Modified over 9 years ago
1
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki wbieniec@kis.p.lodz.pl http://wbieniec.kis.p.lodz.pl
2
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
3 When we declare c to be of type Collection, 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
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 { void add(E x); Iterator iterator(); } public interface Iterator { E next(); boolean hasNext(); }
5
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 and vector 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
6...the following piece of code: List l1 = new ArrayList (); List l2 = new ArrayList (); 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
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
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
9 Bounds = supertype of a type variable (Comparable 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
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
11 Wildcard instantiation
12
12 Wildcards
13
13 Consider a method that draws objects from a class hierarchy of shapes. Cannot draw e.g. a list of circles because List is NOT a subtype of List. Bounded wildcard example
14
14 Fix with ‘?’ ?? Use upper bound wildcard to solve the problem. Bounded wildcard example
15
15 Defining a generic type – case study
16
16 Defining constructors. Naïve approach…
17
17 Defining constructors. Quick and dirty fix...
18
18 Same type constructor argument
19
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
20 Compatible type constructor argument
21
21 Equivalent implementation
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.