Created
November 16, 2012 00:57
-
-
Save barik/4082925 to your computer and use it in GitHub Desktop.
Generics for Donghoon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Step 0: Java Generics | |
Step 1: | |
A lesson on generics can be found here: | |
http://docs.oracle.com/javase/tutorial/java/generics/ | |
JLS is at (search Generic): | |
http://docs.oracle.com/javase/specs/jls/se7/html/index.html | |
Default usage: | |
List list = new ArrayList(); | |
list.add("hello"); | |
String s = (String) list.get(0); | |
to | |
List<String> list = new ArrayList<String>(); | |
list.add("hello"); | |
String s = list.get(0); // no cast | |
General form: | |
ContainerInterface<Type> x = new Container<Type>(); | |
adds type safety. | |
Step 2: | |
Usage of feature: | |
1. Instantiating a Generic Type (default usage): | |
Box<Integer> integerBox; | |
Creating parameterized types: | |
2. Generic class | |
public class Box<T> { | |
... | |
} | |
3. Generic constructor: | |
public Pair(K key, V value) { | |
this.key = key; | |
this.value = value; | |
} | |
4. Generic methods: | |
public void setKey(K key) { this.key = key; } | |
public K getKey() { return key; } | |
5. Bounded types (Extends) | |
public class NaturalNumber<T extends Integer> { | |
... | |
} | |
<T extends B1 & B2 & B3> | |
Multiple bounds: | |
<T extends B1 & B2 & B3> | |
6. Wildcard (bounded and unbounded) | |
public static void process(List<? extends Foo> list) { /* ... */ } | |
Enumerating all the features: K = 6 | |
Step 3: | |
1. For default usage (using generics in container): | |
D1 = 1 | |
2. Generic class: | |
Different usage? Yes, creating generic parameter. | |
Different syntax? No, still using <> | |
Different semantics? Yes, definition/creation of generic parameter. | |
D2 = 1(usage) + 0(syntax) + 2(semantics) = 3 | |
3. Generic constructor | |
Different usage? Yes, having both type and label. | |
Different syntax? No, still using <>. | |
Different semantics? Yes, argument name is now bound to type. | |
D3 = 1(usage) + 0(syntax) + 2(semantics) = 3 | |
4. Generic method | |
Different usage? Yes, for method instead of constructor. | |
Different syntax? No, still using <>. | |
Different semantics? No, same as constructor. | |
D4 = 1(usage) + 0(syntax) + 0(semantics) = 1 | |
5. Bounded types. | |
Different usage? Yes, enhancing classes. | |
Different syntax? Yes, introduction of extends, and &. | |
Different semantics? Yes, requires understanding inheritance. | |
D4 = 1(usage) + 2(syntax) + 2(semantics) = 5 | |
6. Wildcards | |
Different usage? Yes, further capabilities to selecting generics. | |
Different syntax? Yes, introduction of ?. | |
Different semantics? Yes. Changes bounding of types (e.g, relaxing) that | |
is different from simple extends. | |
D4 = 1(usage) + 2(syntax) + 2(semantics) = 5 | |
Overall LOD: 1 + 3 + 3 + 1 + 5 + 5 = 18 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment