P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.

Slides:



Advertisements
Similar presentations
Classes And Objects II. Recall the LightSwitch Class class LightSwitch { boolean on = true; boolean isOn() { return on; } void switch() { on = !on; }
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Java Review Interface, Casting, Generics, Iterator.
Written by: Dr. JJ Shepherd
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
C#: Data Types Based on slides by Joe Hummel. 2 UCN Technology: Computer Science Content: “.NET is designed around the CTS, or Common Type System.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
CS102 Data Types in Java CS 102 Java’s Central Casting.
Static Class Members Wrapper Classes Autoboxing Unboxing.
Miscellaneous OOP topics Java review continued. Simple data types & wrapper classes Simple data types are the built-in types provided as part of the Java.
15-Jul-15 Generics. ArrayList s and arrays A ArrayList is like an array of Object s, but... Arrays use [ ] syntax; ArrayList s use object syntax An ArrayList.
March 2005Java Programming1. March 2005Java Programming2 Why Java? Platform independence Object Oriented design Run-time checks (fewer bugs) Exception.
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
BPJ444: Business Programming using Java Java basics Tim McKenna
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
ArrayList, Multidimensional Arrays
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
Java Basics.  To checkout, use: svn co scb07f12/UTORid  Before starting coding always use: svn update.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Autoboxing A new feature in Java 5.0. Primitive types and classes In Java we have primitive types –boolean, char, byte, short, int, long, float, double.
Types in programming languages1 What are types, and why do we need them?
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Copyright Curt Hill Variables What are they? Why do we need them?
CS-2851 Dr. Mark L. Hornick 1 Generic Java Classes Implementing your own generic classes.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Java 5 Part 2 CSE301 University of Sunderland Harry Erwin, PhD.
Genericity collections of objects. COSC 2006 Bags of Objects2 collections of objects  for real programming applications, pratical collections are collections.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
CSI 3125, Preliminaries, page 1 Data Type, Variables.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
CSE 1201 Object Oriented Programming ArrayList 1.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Typecasting References Computer Science 3 Gerb Reference: Objective: Understand how to use the Object class in Java in the context of ArrayLists.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
The ArrayList Data Structure Standard Arrays at High Speed!
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
April 13, 1998CS102-02Lecture 3-1 Data Types in Java CS Lecture 3-1 Java’s Central Casting.
Outline Creating Objects The String Class Packages Formatting Output
Java Primer 1: Types, Classes and Operators
Java Generics.
5 Variables, Data Types.
Unit-2 Objects and Classes
Wrapper Classes The java.lang package contains wrapper classes that correspond to each primitive type: Primitive Type Wrapper Class byte Byte short Short.
Java Classes and Objects 3rd Lecture
Grouped Data Arrays, and Array Lists.
Recap Week 2 and 3.
ArrayLists 22-Feb-19.
Java Programming Review 1
Java Programming Language
slides created by Alyssa Harding
Java’s Central Casting
Review: libraries and packages
Outline Creating Objects The String Class The Random and Math Classes
Subtype Substitution Principle
Java Coding 6 David Davenport Computer Eng. Dept.,
Presentation transcript:

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

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.

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; “Engage!” String s Object obj

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.”); “Engage!” String s Object obj “Make it so.” … s = (String) obj;

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

Wrapper Classes p Solution: create special classes in which each object holds a primitive type: byteshortintlongfloatdoublecharbooleanByteShortIntegerLongFloatDoubleCharacterBoolean

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

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

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).

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

Multi-type operations p 3 solutions 1. Overloaded methods 2. Use Object type 3. Use generic types

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]; }}

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);

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);

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).

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”

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!

Examples p ArrayBag p ArrayBag2 p 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 p AudioClip AudioClip

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

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

Warning! p Don’t change a list while an iterator is being used

Examples p Lister p ListerBad