Download presentation
Presentation is loading. Please wait.
1
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5
2
Exam will cover Polymorphism and dynamic binding: so review Lecture 11 and run the quiz generator for chapter 9 Interfaces, e.g. Comparable: we reviewed Lecture 17 and 18 and ran the quiz generator for chapter 10 in previous class Exceptions, including text file I/O: review Lecture 23 on your own (we ran the quiz generator for chapter 17 in previous class) Recursion: run quiz generator for chapter 19 Arrays: maybe also run quiz generator for chapter 6
3
Exam will not cover GUI methods Floating point details Sorting algorithm details Generic types
4
boxing: new in Java 1.5 Integer I = 10; // 10 is “boxed” Thus conversions between primitive types and wrapper types is automatic when needed
5
Generic types: new in Java 1.5 Chapter 21 of comprehensive edition of Liang Can declare a generic type in a class, interface or method and specify a concrete type when using it
6
The Comparable Interface in Java 1.5 public interface Comparable { public int compareTo(T o) } previously there was no and instead of “T o” it was “Object o” is a generic type
7
Implementing “max” in Java 1.5 public class Max{ public static >E max(E o1, E o2){ if(o1.compareTo(o2) > 0) return o1; else return o2; } } Our old version of “max” could fail at run time because the objects are not of the same class, e.g. o1 references a String and o2 references an Integer This cannot happen with the new version: such an error will get caught at compile time >E both defines a limitation on the class E (it has to implement Comparable ) and says the return type is E the 2 args also need to have the same type E
8
Implementing Compareable If we want compiler to catch an error when one of the objects references a Date and the other doesn’t, we have to make Date implement Comparable Then the explicit parameter to compareTo must be Date, not Object: this does override the compareTo method of the Object class(?) Now we no longer need a cast – crucial point, as before the cast could have failed at run time
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.