Download presentation
Presentation is loading. Please wait.
Published byAnis Ward Modified over 9 years ago
1
p Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using Java
2
Java’s Object Type p All types (except primitive types) are Object types. p Recall use of Object type (clone and equals methods) p 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
Widening Conversion String s = new String(“Engage!”); Object obj; obj = s; “Engage!” String s Object obj
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.”); “Engage!” String s Object obj “Make it so.” … s = (String) obj;
7
Wrapper Classes p Problem: primitive types are not Object types: p byte, short, int, long, float, double, char, boolean
8
Wrapper Classes p Solution: create special classes in which each object holds a primitive type: byteshortintlongfloatdoublecharbooleanByteShortIntegerLongFloatDoubleCharacterBoolean
9
Wrapper Classes p Each wrapper class has p Constructor p 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 p 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 p Even simple operations take longer because of boxing and unboxing: Integer x = 5; Integer y = 12; Integer z = x + y; AutoboxAuto-unboxAuto-unbox
13
Multi-type operations p 3 solutions 1. Overloaded methods 2. Use Object type 3. Use generic types
14
1. Overloaded Methods static Integer middle(Integer[] data) { if (data.length == 0){ if (data.length == 0){ return null; return null; } else { } else { return data[data.length/2]; return data[data.length/2]; }}
15
1. Overloaded Methods static Character middle(Character[] data) { if (data.length == 0){ if (data.length == 0){ return null; return null; } else { } else { return data[data.length/2]; 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){ if (data.length == 0){ return null; return null; } else { } else { return data[data.length/2]; 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 p Depends on an unspecified underlying data type that is eventually identified when the class is used in an application program. p Enhances type checking capabilities p 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 1. Change the class name p If you used type in the name, remove it 2. Add to all class references (including the class name) within the class itself 3. Change type of the underlying element p Change to E, which stands for “element” 4. Change static methods to generic static p Add after keyword “static”
19
Steps to Convert Collection Class to Generic Class 5. Take care when creating any new E objects p Might need to use Object constructor 6. Modify equality tests p Most == or != must be changed to use equals method 7. Deal with null references appropriately 8. Set unused reference variables to null 9. Don’t forget to update documentation!
20
Examples p ArrayBag p ArrayBag2 p 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 p AudioClip AudioClip
23
Writing a Class to Implement an Interface 1. Read interface documentation 2. Tell the compiler you are implementing an interface public class MP3Player implements AudioClip 3. Implement all methods defined in the interface
24
Generic Interfaces p Like any generic class, a generic interface depends on one or more unspecified classes ( ). p E.g. Iterator p E.g. Iterator Iterator
25
Warning! p Don’t change a list while an iterator is being used
26
Examples p Lister p ListerBad
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.