1 / 18

Generics Chapter 21

Learn how to use generic classes and interfaces, declare generic types, and understand the benefits of using generics in Java programming.

aladd
Download Presentation

Generics Chapter 21

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Generics Chapter 21 Liang, Introduction to Java Programming

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

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

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

  5. Generic Type Generic Instantiation Runtime error b) Improves reliability Compile error Liang, Introduction to Java Programming

  6. Generics contd. The prior slide declares that c is a reference variable whose type is Comparable<Date> 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. 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 <E1, E2, E3> Liang, Introduction to Java Programming

  8. The ArrayList Class in JDK1.5 is now a Generic class Liang, Introduction to Java Programming

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

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

  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<Double> list = new ArrayList<Double>(); 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. 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: Liang, Introduction to Java Programming

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

  14. Creating a Generic Class public class GenericClass<E> { 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. 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<String> stack1 = new GenericStack<String> (); • GenericStack<Integer> stack2 = new GenericStack<Integer> (); Although GenericStack<String> and GenericStack<Integer> are two types, but there is only one class GenericStack loaded into the JVM. Liang, Introduction to Java Programming

  16. Generic methods public static <E> 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.<Integer>print(integers); ClassName.<String>print(strings); Liang, Introduction to Java Programming

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

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

More Related