1 / 16

Generics

Generics. Writing 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. Generic data structures.

dexter
Download Presentation

Generics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Generics

  2. Writing 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.

  3. Generic 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.

  4. Generic 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

  5. Java’s 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 container’s entries: • It breaks Liskov’s substitutability principle. • Client is forced to write and maintain repeated code • It leads to class explosion when container is subclassed.

  6. Java’s 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.

  7. Java’s generic syntax public class List<Entry> { … 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.

  8. Matches the type parameter throughout the class implementation Matches the List constructor’s parameters Generic List instantation • List<Student> studentList = new List<Student>(1,20);

  9. import java.util.Vector; class DynamicList<Entry> { public DynamicList() { elements = new Vector<Entry>(); } 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 Vector<Entry> elements; } DynamicList<Student> studentList = new DynamicList<Student>( ); //constructors arguments if any,

  11. Generic methods • Using the Java’s generic extension we can also write methods whose algorithm does not depend on explicit types: • To swap two entries in an array. <Element> void swap(Element[] a, int i1, int i2){ Element temp = a[i1]; a[i1] = a[i2]; a[i2] = temp; } An invocation: swap(integersTable, 3, 7);

  12. More examples of generic functions Interface Function<A,B> { B value(A arg); } Interface Ntuple<T> { <S> Ntuple<S> map( Function<A,B> f); } Ntuple<Integer> nti = ….; nti.map( new Function<Integer, Integer>{ Integer value(Integer i){ return new Integer(i.intValue()*2); } } );

  13. Java’s generic • Classes • Abstract classes • Interfaces • methods

  14. Java’s 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.

  15. How can I start using generics? • For home use follow the link to: http://www.cs.uno.edu/~c2120001/Utilities/ 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.

  16. Java 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/scripts 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

More Related