Download presentation
Presentation is loading. Please wait.
Published byClaud Holt Modified over 9 years ago
1
Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?
2
Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?
4
Generic Types Recent release of Java added 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 String as actual type Code runs as if were written using that type Type used by instance cannot be changed Type parameter becomes part of variable’s type
6
Generics Before & After Before GenericsAfter Generics
7
Writing Generics
11
See Generics Behave
12
For earth, class written as if T were replaced by Integer
13
See Generics Behave
14
For matchbox, T is Car This can be at same time T is Integer when for earth
15
Using Generics Without GenericsWith 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 n; Bag m;... n = new Bag (5,“B”); i = n.getData(); c = n.getData(); n.setData(c); m = new Bag (c, “B”); i = m.getData(); c = m.getData();
16
Using Generics Without GenericsWith 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 n; Bag m;... n = new Bag (5,“B”); i = n.getData(); c = n.getData(); n.setData(c); m = new Bag (c, “B”); i = m.getData(); c = m.getData();
17
Using Generics Without GenericsWith 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 n; Bag m;... n = new Bag (5,“B”); i = n.getData(); c = n.getData(); n.setData(c); m = new Bag (c, “B”); i = m.getData(); c = m.getData();
18
Using Generics Without GenericsWith 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 n; Bag m;... n = new Bag (5,“B”); i = n.getData(); c = n.getData(); n.setData(c); m = new Bag (c, “B”); i = m.getData(); c = m.getData();
19
Using Generics Without GenericsWith 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 n; Bag m;... n = new Bag (5,“B”); i = n.getData(); c = n.getData(); n.setData(c); m = new Bag (c, “B”); i = m.getData(); c = m.getData();
20
Using Generics Without GenericsWith 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 n; Bag m;... n = new Bag (5,“B”); i = n.getData(); c = n.getData(); n.setData(c); m = new Bag (c, “B”); i = m.getData(); c = m.getData();
21
Using Generics Without GenericsWith 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 n; Bag m;... n = new Bag (5,“B”); i = n.getData(); c = n.getData(); n.setData(c); m = new Bag (c, “B”); i = m.getData(); c = m.getData();
22
public class Entry { private S key; private TYPE value; // And more goes here... } Entry a; Entry b; Entry c; Entry d; Entry e; Can Use Multiple Generic Types
23
public class Entry { private S key; private TYPE value; // And more goes here... } Entry a; Entry b; Did not specify for each type Entry c; Entry d; Entry e; Can Use Multiple Generic Types
24
public class Entry { private S key; private TYPE value; // And more goes here... } Entry a; Entry b; Did not specify for each type Entry c; Entry d; Not reference type Entry e; Can Use Multiple Generic Types
25
When To Specify Type Whenever class name used (except constructors) Variable declarations: ArrayList hogCount; Object instantiation: hogCount = new ArrayList (); Return type for method : private ArrayList transport() Parameter listing: public void cook(ArrayList fd) Used as type parameter: ArrayList > bacon;
26
Type cannot be specified instantiating array Compiler error if type specified during instantiation Can provide type theory explaining this problem Generics Annoyance
27
Type cannot be specified instantiating array Can use generics with arrays, but need typecast Only needed once, use generics after instantiation Still checks when compiling, so get most benefits public class Farm { private ArrayList [] troughs; private T[] animals; public Farm() { troughs = (ArrayList [])new ArrayList[10]; animals = (T[])new Object[1034821]; } } Generics Annoyance
28
In Case of Unknown Type
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
38
Your Turn Get into your groups and complete activity
39
For Midterm You can use on this midterm: Your textbook & notes IF Printout of slides IF has notes on that day's slides At the same time, you may NOT use: Computer, calculator, cell phone, or similar Copies of daily activities and/or solutions Friends, Romans, countrymen or their ears To be certain rules are followed, when test ends Hand in all printed material you had with you
40
How to Prepare for Midterm DODON'T Make cheat sheets for the test Review how parts of Java work Add post-its to important pages Memorize Drink case of 40s before test Use post-its as clothing
41
For Next Lecture Reviewing OO Programming on Monday Come prepared to ask any questions you still have Will have some fun & interesting challenges in class See how prepare you are for Wednesday's midterm
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.