1 / 26

Generic Programming

Generic Programming. Object type and wrapper classes Object methods Generic classes Interfaces and iterators. Data Structures and Other Objects Using Java. Java’s Object Type. All types (except primitive types) are Object types. Recall use of Object type (clone and equals methods)

jtipton
Download Presentation

Generic Programming

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. Generic Programming • Object type and wrapper classes • Object methods • Generic classes • Interfaces and iterators Data Structures and Other Objects Using Java

  2. Java’s Object Type • All types (except primitive types) are Object types. • Recall use of Object type (clone and equals methods) • An Object variable is capable of holding a reference to any kind of object.

  3. Widening Conversion An assignment x = y is widening if the data type of x is capable of referring to a wider variety of things than the type of y. int y = 42; double x; x = y;

  4. String s Object obj “Engage!” Widening Conversion String s = new String(“Engage!”); Object obj; obj = s;

  5. Narrowing Conversions Narrowing conversions generally require an explicit typecast. int x; double y = 3.14; x = (int) y;

  6. Narrowing Conversions String s = new String(“Engage!”); Object obj; obj = s; s = new String(“Make it so.”); … s = (String) obj; String s Object obj “Make it so.” “Engage!”

  7. Wrapper Classes • Problem: primitive types are not Object types: • byte, short, int, long, float, double, char, boolean

  8. Wrapper Classes • Solution: create special classes in which each object holds a primitive type: byte short int long float double char boolean Byte Short Integer Long Float Double Character Boolean

  9. Wrapper Classes • Each wrapper class has • Constructor • intValue method int i = 42; int j; Integer example; example = new Integer(i); j = example.intValue( ); boxing unboxing

  10. Autoboxing and Auto-unboxing int i = 42; int j; Integer example; example = i; j = example; Autoboxing Auto-unboxing

  11. Advantage of Wrappers • The wrapper object is a full Java object, so we can use it in ways that we can’t use the primitive types (e.g. for generics, as we will see).

  12. Disadvantage of Wrappers • Even simple operations take longer because of boxing and unboxing: Integer x = 5; Integer y = 12; Integer z = x + y; Autobox Auto-unbox Auto-unbox

  13. Multi-type operations • 3 solutions • Overloaded methods • Use Object type • Use generic types

  14. 1. Overloaded Methods static Integer middle(Integer[] data) { if (data.length == 0){ return null; } else { return data[data.length/2]; } }

  15. 1. Overloaded Methods static Character middle(Character[] data) { if (data.length == 0){ return null; } else { return data[data.length/2]; } } It does the job, but it’s a lot of work creating methods for every possible type: i = middle(ia);

  16. 2. Object Methods static Object middle(Object[] data) { if (data.length == 0){ return null; } else { return data[data.length/2]; } } Also does the job, but the call is awkward, and certain type errors can’t be found by the compiler: i = (Integer) middle(ia);

  17. 3. Generic Classes • Depends on an unspecified underlying data type that is eventually identified when the class is used in an application program. • Enhances type checking capabilities • The type used to instantiate the generic type must be a class (not a primitive type…for primitives, use wrapper class).

  18. Steps to Convert Collection Class to Generic Class • Change the class name • If you used type in the name, remove it • Add <E> to all class references (including the class name) within the class itself • Change type of the underlying element • Change to E, which stands for “element” • Change static methods to generic static • Add <E> after keyword “static”

  19. Steps to Convert Collection Class to Generic Class • Take care when creating any new E objects • Might need to use Object constructor • Modify equality tests • Most == or != must be changed to use equals method • Deal with null references appropriately • Set unused reference variables to null • Don’t forget to update documentation!

  20. Examples • ArrayBag • ArrayBag2 • Node

  21. Java Interfaces An interface is a formal outline of the available methods for some class. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

  22. Example • AudioClip

  23. Writing a Class to Implement an Interface • Read interface documentation • Tell the compiler you are implementing an interface public class MP3Player implements AudioClip • Implement all methods defined in the interface

  24. Generic Interfaces • Like any generic class, a generic interface depends on one or more unspecified classes (<E>). • E.g. Iterator<E>

  25. Warning! • Don’t change a list while an iterator is being used

  26. Examples • Lister • ListerBad

More Related