Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Generics Chapter 21 Liang, Introduction to Java Programming.

Similar presentations


Presentation on theme: "1 Generics Chapter 21 Liang, Introduction to Java Programming."— Presentation transcript:

1 1 Generics Chapter 21 Liang, Introduction to Java Programming

2 2 Place these programs and their classes from folder Generics on the desktop GenericMethodDemo GenericClass.java GenericClass2.java Liang, Introduction to Java Programming

3 3 Objectives F To use generic classes and interfaces from the API F To declare generic classes and interfaces F To understand why generic types can improve reliability and robustness F To declare and use generic methods F To know wildcard types and understand why they are necessary Liang, Introduction to Java Programming

4 4 Introduction F Generic is the capability to parameterize types F You can declare a generic type in a class, interface, or method F You can specify a concrete type when using the generic class, interface or methods F Generic types can improve readability, reliability and robustness for the program F The new JDK 1.5 generic types provide a mechanism that supports type checking at compile time Liang, Introduction to Java Programming

5 5 Generic Type Generic Instantiation Runtime error Compile error b) Improves reliability

6 Liang, Introduction to Java Programming 6 Generics contd. The prior slide declares that c is a reference variable whose type is Comparable in JDK 1.5 and invokes the compareTo method to compare a Date object with a string. The code has a compile error, because the argument passed to the compareTo method must be of the Date type. Since the errors can be detected at compile time rather than at runtime. The generic type makes the program more reliable.

7 Liang, Introduction to Java Programming 7 Generics contd. Caution: Generic type must be reference types. You cannot substitute a generic type with a primitive such as int, double, or char, float, etc. However, you can use wrapper classes such as Integer, Double, Character, etc instead. Note: Occasionally, a generic class may have more than one parameter. In this case, place the parameters together inside the angle brackets, separated by commas such as

8 8 The ArrayList Class in JDK1.5 is now a Generic class Java.util.ArrayList +ArrayList() +add(E o): booleanaddE +add(int index, E element) : voidadd +clear() : void clear +clone() : Object clone +get(int index) : Eget +indexOf(Object elem) : intindexOfObject +set(int index, E element) : Eset Liang, Introduction to Java Programming

9 99 ArrayList The ArrayList class is a generic class. You can form array lists that collect different types, such as ArrayList, ArrayList and so on Liang, Introduction to Java Programming

10 10 Create an ArrayList F Create a list for a String ArrayList list = new ArrayList (); F Add only strings to the list list.add(“red”); F The following statement will generate an error list.add(new Integer(1)); because list can only contain strings Liang, Introduction to Java Programming

11 11 No Casting Needed Casting is not needed to retrieve a value from a list with a specified element type because the compilier already knows the element type: ArrayList list = new ArrayList (); list.add(5.5); // 5.5 is automatically converted to new Double(5.5) list.add(3.0); // 3.0 is automatically converted to new Double(3.0) Double doubleObject = list.get(0); // No casting is needed double d = list.get(1); // Automatically converted to double Liang, Introduction to Java Programming

12 12 Implementing Generic Classes We need to give names to the type variables. It is consider good form to give short uppercase names for type variables, such as the following: Type Variable NameMeaning EElement type in a collection KKey type in a map VValue type in a map TGeneral type S,UAdditional general types Liang, Introduction to Java Programming

13 13 Syntax for Defining a Generic Class accessSpecifier class GenericClassName <TypeVariable1, TypeVariable2,…> { constructors methods fields } Liang, Introduction to Java Programming

14 14 Creating a Generic Class public class GenericClass { private E[] element; //an array to store elements private int size; //default constructor public GenericClass() { element = (E[]) new Object[10];//set physical size of 10 size = 0; } Liang, Introduction to Java Programming

15 15 Important Facts It is important to note that a generic class is shared by all its instances (objects) regardless of its actual generic type. GenericStack stack1 = new GenericStack (); GenericStack stack2 = new GenericStack (); Although GenericStack and GenericStack are two types, but there is only one class GenericStack loaded into the JVM. Liang, Introduction to Java Programming

16 16 Generic methods public static void print (E[] list) { for (int i =0; i < list.length; i++) System.out.print(list[i] + " "); System.out.println(); } In main: Integer[] integers = {1, 2, 3, 4, 5}; String[] strings = {"London", "Paris", "New York" }; ClassName. print(integers); ClassName. print(strings); Liang, Introduction to Java Programming

17 17 Example F Run the program : GenericClass.java Liang, Introduction to Java Programming

18 18 Wild Card F To create a generic method, a wild card must be used F The wild card argument is specified by the ? F The wild card specify the unknown type F Run program GenericClass2.java Liang, Introduction to Java Programming


Download ppt "1 Generics Chapter 21 Liang, Introduction to Java Programming."

Similar presentations


Ads by Google