1 / 39

Question of the Day

Question of the Day. Move one matchstick to produce a square. Question of the Day. Move one matchstick to produce a square. CSC 212 – Data Structures. Lecture 12: Generic Types. Generic Types. Starting with 1.5 release, Java uses generics Include type parameters in class definition

hope
Download Presentation

Question of the Day

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. Question of the Day • Move one matchstick to produce a square

  2. Question of the Day • Move one matchstick to produce a square

  3. CSC 212 – Data Structures Lecture 12:Generic Types

  4. Generic Types • Starting with 1.5 release, Java uses generics • Include type parameters in class definition • Like methods, parameters can change each time • Fields independent of types can now be written

  5. Generic Types • On allocating instance, actual type is specified • Must be reference type or Stringas actual type • Code runs as if were written using that type • Type used by instancecannot be changed • Type parameter becomes part of variable’s type

  6. Generics Before & After Before Generics After Generics public class ONode {private Objectdata;ONode(Object d) {data = d;}ObjectgetData() { return data;}void setData(Objectd){data = d;} } public class Node<T> {private Tdata;public Node(T d) {data = d;}TgetData() { return data;}void setData(Td){data = d;} }

  7. Writing Generics public class Bag<T, String>{private Tdata;private String name;public Bag(T d, String newName) {data = d;name = newName;}public TgetData() { return data; }public void setData(Td){ data = d; }public String toString() { return name + “: ” + data.toString();} }

  8. Writing Generics public class Bag<T,String>{private Tdata;private Stringname;public Bag(T d, StringnewName) {data = d;name = newName;}public TgetData() { return data; }public void setData(Td){ data = d; }public StringtoString() { return name + “: ” + data.toString();} }

  9. Writing Generics public class Bag<T, String>{private Tdata;private String name;public Bag(T d, String newName) {data = d;name = newName;}public TgetData() { return data; }public void setData(Td){ data = d; }public String toString() { return name + “: ” + data.toString();} }

  10. Writing Generics public class Bag<T> {private Tdata;private String name;public Bag(T d, String newName) {data = d;name = newName;}public TgetData() { return data; }public void setData(Td){ data = d; }public String toString() { return name + “: ” + data.toString();} }

  11. See Generics Behave Bag<Integer> earth = new Bag<Integer>(4, “The Answer”); public class Bag<T> {private Tdata;private String name;public Bag(T d, String newName) {data = d;name = newName;}public TgetData() { return data; }public void setData(Td){ data = d; }public String toString() { return name + “: ” + data.toString();} }

  12. See Generics Behave For earth, class written as if T were replaced by Integer Bag<Integer> earth = new Bag<Integer>(4, “The Answer”); public class Bag {private Integerdata;private String name;public Bag(Integer d, String newName) {data = d;name = newName;}public IntegergetData() { return data; }public void setData(Integerd){ data = d; }public String toString() { return name + “: ” + data.toString();} }

  13. See Generics Behave Bag<Car> matchbox = new Bag<Car>(Z4, “Dream”); public class Bag<T> {private Tdata;private String name;public Bag(T d, String newName) {data = d;name = newName;}public TgetData() { return data; }public void setData(Td){ data = d; }public String toString() { return name + “: ” + data.toString();} }

  14. See Generics Behave For matchbox, T is Car This can be at same time T is Integer when for earth Bag<Car> matchbox = new Bag<Car>(Z4, “Dream”); public class Bag {private Cardata;private String name;public Bag(Car d, String newName) {data = d;name = newName;}public CargetData() { return data; }public void setData(Card){ data = d; }public String toString() { return name + “: ” + data.toString();} }

  15. Using Generics Without Generics With Generics Integer i;Car c;Bag n;...n = new Bag(5,“B”);i = ((Integer)n.getData());c = ((Car)n.getData());n.setData(c);i = ((Integer)n.getData()); c = ((Car)n.getData()); Integer i;Car c;Bag<Integer> n;Bag<Car> m;...n = new Bag<Integer>(5,“B”);i= n.getData();c = n.getData();n.setData(c);m = new Bag<Car>(c, “B”);i= m.getData();c = m.getData();

  16. Using Generics Without Generics With Generics Integer i;Car c;Bag n;...n = new Bag(5,“B”);i = ((Integer)n.getData());c = ((Car)n.getData());n.setData(c);i = ((Integer)n.getData()); c = ((Car)n.getData()); Integer i;Car c;Bag<Integer> n;Bag<Car> m;...n = new Bag<Integer>(5,“B”);i= n.getData();c = n.getData();n.setData(c);m = new Bag<Car>(c, “B”);i= m.getData();c = m.getData();

  17. Using Generics Without Generics With Generics Integer i;Car c;Bag n;...n = new Bag(5,“B”);i = ((Integer)n.getData());c = ((Car)n.getData());n.setData(c);i = ((Integer)n.getData()); c = ((Car)n.getData()); Integer i;Car c;Bag<Integer> n;Bag<Car> m;...n = new Bag<Integer>(5,“B”);i= n.getData();c = n.getData();n.setData(c);m = new Bag<Car>(c, “B”);i= m.getData();c = m.getData();

  18. Using Generics Without Generics With Generics Integer i;Car c;Bag n;...n = new Bag(5,“B”);i = ((Integer)n.getData());c = ((Car)n.getData());n.setData(c);i = ((Integer)n.getData()); c = ((Car)n.getData()); Integer i;Car c;Bag<Integer> n;Bag<Car> m;...n = new Bag<Integer>(5,“B”);i = n.getData();c = n.getData();n.setData(c);m = new Bag<Car>(c, “B”);i = m.getData();c = m.getData();

  19. Using Generics Without Generics With Generics Integer i;Car c;Bag n;...n = new Bag(5,“B”);i = ((Integer)n.getData());c = ((Car)n.getData());n.setData(c);i = ((Integer)n.getData()); c = ((Car)n.getData()); Integer i;Car c;Bag<Integer> n;Bag<Car> m;...n = new Bag<Integer>(5,“B”);i = n.getData();c = n.getData();n.setData(c);m = new Bag<Car>(c, “B”);i = m.getData();c = m.getData();

  20. Using Generics Without Generics With Generics Integer i;Car c;Bag n;...n = new Bag(5,“B”);i = ((Integer)n.getData());c = ((Car)n.getData());n.setData(c);i = ((Integer)n.getData());c = ((Car)n.getData()); Integer i;Car c;Bag<Integer> n;Bag<Car> m;...n = new Bag<Integer>(5,“B”);i = n.getData();c = n.getData();n.setData(c);m = new Bag<Car>(c, “B”);i = m.getData();c = m.getData();

  21. Using Generics Without Generics With Generics Integer i;Car c;Bag n;...n = new Bag(5,“B”);i = ((Integer)n.getData());c = ((Car)n.getData());n.setData(c);i = ((Integer)n.getData());c = ((Car)n.getData()); Integer i;Car c;Bag<Integer> n;Bag<Car> m;...n = new Bag<Integer>(5,“B”);i = n.getData();c = n.getData();n.setData(c);m = new Bag<Car>(c, “B”);i = m.getData();c = m.getData();

  22. Can Use Multiple Generic Types public class Entry<MONKEY, TYPE> { private MONKEYkey; private TYPE value;// And more goes here... } Entry<String, Integer> a; Entry<String> b; Entry<String, String> c; Entry<String, boolean> d; Entry<Car, Boolean> e;

  23. Can Use Multiple Generic Types public class Entry<MONKEY, TYPE> { private MONKEYkey; private TYPE value;// And more goes here... } Entry<String, Integer> a; Entry<String> b;Did not specify for each type Entry<String, String> c; Entry<String, boolean> d; Entry<Car, Boolean> e;

  24. Can Use Multiple Generic Types public class Entry<MONKEY, TYPE> { private MONKEYkey; private TYPE value;// And more goes here... } Entry<String, Integer> a; Entry<String> b;Did not specify for each type Entry<String, String> c; Entry<String, boolean> d; Not reference type Entry<Car, Boolean> e;

  25. When To Specify Type • Whenever class name used(except constructors) • Variable declarations:ArrayList<Integer> hogCount; • Object instantiation:hogCount = new ArrayList<Double>(); • Return type for method :private ArrayList<Pig> transport() • Parameter listing:public void cook(ArrayList<Meat> fd) • Used as type parameter:ArrayList<ArrayList<Meat>> bacon;

  26. Generics Annoyance • Type cannot be specified instantiatingarray • Compiler error if type specified during instantiation • Can provide type theory explaining this problem

  27. Generics Annoyance • Type cannot be specified instantiatingarray • Can use generics with arrays, but need typecast • Only needed once, use generics after instantiation • Still checks when compiling, so get most benefitspublic class Farm<T> { private ArrayList<Feed>[] troughs; private T[] animals; public Farm() {troughs= (ArrayList<Feed>[])new ArrayList[10];animals = (T[])new Object[1034821]; }}

  28. In Case of Unknown Type • Single method or class may not care about type • Different than using typecasts to get to work • Only will work if does not need to instantiate object • Do not skip specifying type -- that is BAD IDEA™ • Instead use the generic wildcard ?

  29. In Case of Unknown Type • Single method or class may not care about type • Different than using typecasts to get to work • Only will work if does not need to instantiate object • Do not skip specifying type -- that is BAD IDEA™ • Instead use the generic wildcard ?

  30. Wildcard in Generic public class ListHolder {private ArrayList<?> myList;public void setList(ArrayList<?> lst){myList= lst;}public void printListSize() {System.out.println(myList.size());}public ArrayList<?> getList() { return myList;} }

  31. Wildcard in Generic public class ListHolder {private ArrayList<?> myList;public void setList(ArrayList<?> lst){myList= lst;}public void printListSize() {System.out.println(myList.size());}public ArrayList<?> getList() { return myList;} } ?matches any reference type(and String)

  32. Wildcard in Generic public class ListHolder {private ArrayList<?> myList;public void setList(ArrayList<?> lst){myList= lst;}public void printListSize() {System.out.println(myList.size());}public ArrayList<?> getList() { return myList;} } Any ArrayList can be passed in for lst

  33. Wildcard in Generic public class ListHolder {private ArrayList<?> myList;public void setList(ArrayList<?> lst){myList= lst;}public void printListSize() {System.out.println(myList.size());}public ArrayList<?> getList() { return myList;} } Can call methods as long as missing type not important

  34. Wildcard in Generic public class ListHolder {private ArrayList<?> myList;public void setList(ArrayList<?> lst){myList= lst;}public void printListSize() {System.out.println(myList.size());}public ArrayList<?> getList() { return myList;} } Legal, but yucky. All type information is lost!

  35. Typecasting Explained

  36. Typecasting Explained

  37. Typecasting Explained • 99% of typecasts are incorrect • 90% fix missing generic typespecification on variable • Eclipse “Quick-Fix” on illegal code is 9% • 0.95% instantiate arrays of generic type • When using interfaces in an ADT is 0.05%

  38. Your Turn • Get into your groups and complete activity

  39. For Next Lecture • Midterm #1 next Monday for duration of class • Will be doing review on Friday, so bring your questions • Object-orientation quiz in class Wednesday • Will be just like quiz in class last week • Inheritance, chains, generics & more covered on quiz • As usual, there is weekly assignment on Angel • Due by 5PM tomorrow via Assignment Submitter • Problem #2 graded using provided JUnit tests • No week #5 assignment due to midterm

More Related