Data Structures and Other Objects

Download Report

Transcript Data Structures and Other Objects

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;
Widening Conversion
String s = new String(“Engage!”);
Object obj;
obj = s;
String s
Object obj
“Engage!”
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;
“Make it so.”
String s
Object obj
“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
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
char
Character
boolean
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

1.
2.
3.
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
1.
Change the class name

2.
3.
Add <E> to all class references (including
the class name) within the class itself
Change type of the underlying element

4.
If you used type in the name, remove it
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
5.
Take care when creating any new E objects

6.
Modify equality tests

7.
8.
9.
Might need to use Object constructor
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
1.
2.
Read interface documentation
Tell the compiler you are implementing an
interface
public class MP3Player implements AudioClip
3.
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
used
change a list while an iterator is being
Examples
 Lister
 ListerBad