Download presentation
Presentation is loading. Please wait.
Published byDwight Dalton Modified over 9 years ago
1
Generic Programming Object Type Autoboxing Bag of Objects JCL Collections Nodes of Objects Iterators
2
Ch. 5 Java Objects and Iterators Java’s Object type Wrapper classes for primitive data types Objects collection is actually references collection Node object that contains object data Iterator
3
Need for Java’s Object type and generics Bag Sequence Stack ADTs and data structures where essentially the same design can be used for data of different types (or even mixed types) for each type re-write essentially the same code minimal modification with respect to the type for this code
4
Java’s Object type Java’s Object class type provides a possibility for writing one code and using it for any type (or all types
5
Object class and inheritance Every class in Java is directly or indirectly (if the class extends another class) derived from the Object class,, i.e., extending the Object class. So, every object is an object of Object class String s=new String(“Something”); Object obj; obj = s;
6
Class type conversion If an object is being treated as an object of Object class, using this object reference we can not access the methods and member variables defined in its original class. The reference needs to be converted to the original type by casting String s=new String(“Something”); Object obj; obj = s; s= (String) obj;
7
Conversion of object reference Widening conversion example: String str = new String (“Hello, World!”); String str = new String (“Hello, World!”); Object obj = str; Object obj = str; int stringLength = str.length( ); int stringLength = str.length( ); stringLength = obj.length( ); stringLength = obj.length( ); Narrowing conversion example: String string = (String) obj; String string = (String) obj; stringLength = string.length( ); stringLength = string.length( );
8
Java 1.5 Autoboxing Java has 8 primitive (non-object) types of data –integers: int, long, short, byte –fractionals: double, float –logicals: boolean –characters: char
9
Wrapper Classes There are 8 classes in Java specifically for making an object out of a value of any of the primitive data type, and providing additional processing tools: Integer Float Long Boolean Short Character ByteDouble
10
Boxing and Unboxing Autoboxing conversion – primitive value converted to a wrapper object of corresponding type Unboxing – wrapper object is converted to corresponding primitve
11
Autoboxing and Unboxing //boxing int i = 42; int j; Integer example; Example = new Integer(i); j = example.intValue(); //autoboxing example=i; //autoboxing j=example; //autounboxing
12
Objects example Really collection of references to objects class ArrayBag object is a bag of objects of any type implemented on an array of Object type Object [ ] data;
13
Java Objects Java Objects A collection of objects is actually a collection of references to objects Each element of the data array is a reference to an object Some of them may refer to the same object:
14
Same Object reference example add the same reference into the bag several times each time a copy of the reference is stored into a different position of the array all are references to the same object, no new copy of the actual object has ever been made
15
object equality == and != tests for objects only test if two references refer to the same object not if they have the same content two distinct objects can have the same content. provide test of equality of contents, write an equals method in which the object contents are compared
16
null value legal value for any class type most primitive data types have nothing similar. code with objects needs to deal with it source of NullPointer-Exception frustration, or error in answers
17
Node object that contain object data A node object can be used in a linked list Instance variables of the IntNode class private int data; private int data; private IntNode link; private IntNode link; Instance variables of the general Node class private Object data; private Object data; private IntNode link; private IntNode link;
18
Generic method vs Object Method static Object middle(Object[]data) { if (data.length ==) if (data.length ==) {return null;} {return null;} else else {return {return data[data.length];} data[data.length];}} static T middle(T []data) { if (data.length ==) {return null;} else {return data[data.length];} }
19
Generic method restrictions Data type inferred must always be a class type (not a primitive) May not call a constructor for the generic type Nor may you create a new array of element of that type
20
GENERIC CLASSES public class ArrayBag implements Cloneable { private E[] data; private int manyItems; private int manyItems;} ArrayBag sbag= new ArrayBag ();
21
Generic Class Restrictions Type used to instantiate must be a class [not primitive] Within class can’t call constructor for generic type (nor make array of elements of that type)
22
Array Bag A generic bag class http://southwestern.edu/~owensb/CS2Sp08/Lectures/NewBag/ArrayBag.java A silly program to use it http://southwestern.edu/~owensb/CS2Sp08/Lectures/NewBag/Author.java
23
Generic Node public class IntNode { private int data; private int data; IntNode linke IntNode linke} public class Node { private E data; Node link }
24
Equals public static Node listSearch(Node head, E target) public static Node listSearch(Node head, E target) { Node cursor; Node cursor; if (target == null) if (target == null) { // Search for a node in which the data is the null reference. { // Search for a node in which the data is the null reference. for (cursor = head; cursor != null; cursor = cursor.link) for (cursor = head; cursor != null; cursor = cursor.link) if (cursor.data == null) if (cursor.data == null) return cursor; return cursor; } else else { // Search for a node that contains the non-null target. { // Search for a node that contains the non-null target. for (cursor = head; cursor != null; cursor = cursor.link) for (cursor = head; cursor != null; cursor = cursor.link) if (target.equals(cursor.data)) if (target.equals(cursor.data)) return cursor; return cursor; } return null; return null; }
25
Iterator and Enumeration
26
Iterator Code A class that implements the generic Iterator must provide these three methods public boolean hasNext ( ); public boolean hasNext ( ); public E next ( ); public E next ( ); public void remove ( ); public void remove ( );
27
Interface vs. Class ` `interface methods are un-implemented originally ` ` objects of an interface cannot be created, until some class implements this interface and its methods ` `then an object of this class can be used as an object of this interface
28
Interface common standard shared between the user/invoker of Java programs and the program designer documentation of an interface specifies clearly the properties of an object of this interface whatever class the object belongs user can use it confident of those properties
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.