270 likes | 290 Views
Learn about Java object types, widening and narrowing conversions, wrapper classes, autoboxing, unboxing, and generic classes in Java. Discover how to convert collection classes to generic classes and understand interfaces in Java programming.
E N D
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) • An Object variable is capable of holding a reference to any kind of object.
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;
String s Object obj “Engage!” Widening Conversion String s = new String(“Engage!”); Object obj; obj = s;
Narrowing Conversions Narrowing conversions generally require an explicit typecast. int x; double y = 3.14; x = (int) y;
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!”
Wrapper Classes • Problem: primitive types are not Object types: • byte, short, int, long, float, double, char, boolean
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
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
Autoboxing and Auto-unboxing int i = 42; int j; Integer example; example = i; j = example; Autoboxing Auto-unboxing
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).
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
Multi-type operations • 3 solutions • Overloaded methods • Use Object type • Use generic types
1. Overloaded Methods static Integer middle(Integer[] data) { if (data.length == 0){ return null; } else { return data[data.length/2]; } }
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);
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);
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).
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”
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!
Examples • ArrayBag • ArrayBag2 • Node
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.
Example • AudioClip
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
Generic Interfaces • Like any generic class, a generic interface depends on one or more unspecified classes (<E>). • E.g. Iterator<E>
Warning! • Don’t change a list while an iterator is being used
Examples • Lister • ListerBad