Title: Generics
1Generics
2Writing typed checked generic code
- There are many instances where the type of the
object is irrelevant - The construction of data structures.
- The specification and implementation of methods.
3Generic data structures
- Containers are and ought to be independent of the
type of their entries. - A list specification and implementation is the
same regardless of the type of its entries.
4Generic operations
- There are many methods whose algorithm is and
ought to be independent of the type of its
parameters - Swapping
- Reversing a list
- Length
- Spliting a list
- Concatenating two lists
- Sorting
5Javas old solution
- For containers
- Use Object as the generic type.
- This solution has only negative consequences
- Elements added to a container cannot be typed
checked for its true type i.e. we loose type
checking. - Client of containers must coerce at run-time to
the expected type - instanceof is an expensive operation.
- Client may coerce to the incorrect type.
- Without use of instanceof client may run into
run-time errors. - For production of safe code programmers must
define abstract containers based on Object and
subclass it for the expected type of containers
entries - It breaks Liskovs substitutability principle.
- Client is forced to write and maintain repeated
code - It leads to class explosion when container is
subclassed.
6Javas new solution Generics. What are generics?
- Generics allows you to abstract over types.
- Specifically you can specify parameters whose
values are types. - Example The only difference between the
different XXXList is the type of the entries. - Specify the list using type as a parameter.
- Implement the list using the parameter wherever
you need a specific type. - When you need a specific list (a list of
students) - Instantiate the List matching the parameter type
with Student.
7Javas generic syntax
- public class ListltEntrygt
-
- public void add(Entry element)
- public Entry get(int index) ..
-
- private Entry elements
-
- Entry in angular brackets represents a type
variable, to be matched by an existing class when
the generic class is instantiated.
8Generic List instantation
- ListltStudentgt studentList new
ListltStudentgt(1,20)
9import java.util.Vector class
DynamicListltEntrygt public DynamicList()
elements new VectorltEntrygt()
public boolean isEmpty() return
elements.size() 0 public Entry
get(int i) return elements.get(i)
public int indexOf( Entry entry)
return elements.indexOf(entry)
public void append (Entry entry)
elements.add(entry)
10 public void add(int i, Entry entry)
elements.add(i, entry) public
void remove(int i) elements.removeElementAt
(i) public int size() return
elements.size() public void set (
int i, Entry entry) elements.set(i,entry)
private VectorltEntrygt
elements DynamicListltStudentgt studentList
new DynamicListltStudentgt( ) //constructors
arguments if any,
11Generic methods
- Using the Javas generic extension we can also
write methods whose algorithm does not depend on
explicit types - To swap two entries in an array.
- ltElementgt void swap(Element a, int i1, int i2)
- Element temp ai1
- ai1 ai2
- ai2 temp
-
- An invocation swap(integersTable, 3, 7)
12More examples of generic functions
- Interface FunctionltA,Bgt
- B value(A arg)
-
- Interface NtupleltTgt
- ltSgt NtupleltSgt map( FunctionltA,Bgt f)
-
- NtupleltIntegergt nti .
- nti.map( new FunctionltInteger, Integergt
- Integer value(Integer i)
- return new Integer(i.intValue()2)
-
-
- )
13Javas generic
- Classes
- Abstract classes
- Interfaces
- methods
14Javas generic trade-offs
- Positive
- The way to catch type errors up front.
- Code is more readable.
- Client does not have to coerce.
- Totally compatible with current Java
- Negative
- The choice of implementation of generic code
- may produce unexpected warning errors.
- Cannot overload based on different instantiations
of same generic type.
15How can I start using generics?
- For home use follow the link to
- http//www.cs.uno.edu/c2120001/Utiliti
es/ - download jdk1.5 extensions (zip file)
- README file instructs you for setup and
compilation of generic code - Need to set two environment variables
- use the javac version provided to compile
generic code. - The usual javac will not compile it.
- The command to run the interpreter (java)
will also need to be changed to use new generic
libraries.
16Java generics in the dept
- To run the compiler from the department network,
set the environment - variables J2SE14 and JSR14DISTR.
- export J2SE14/usr/java
- export JSR14DISTR/home/c2120001/Utilities/adding_
generics-2_2-ea - and run the scripts javac and java from
/home/c2120001/Utilities/adding_generics-2_2-ea/sc
ripts to compile and execute. - To simplify all the above, two scripts to run the
compiler and interpreter, javac1.5 and java1.5,
are available in /home/c2120001/scripts. You can
simply copy these scripts to your home directory
and use them to compile and run. For example, - /javac1.5 foo.java
- /java1.5 foo