generics in java - westmont...
TRANSCRIPT
![Page 1: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/1.jpg)
GENERICS IN JAVAComputer Science II CS 030 Donald J. Patterson
![Page 2: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/2.jpg)
GENERICS FROM 10,000 FEET
• Generics enable types (classes and interfaces) to be
parameterized.
• The input to Generics are types
• The output of Generics are new types
• They are like parameters in methods
• but they are about types, not data, not values
• they are meta-data
• They let you re-use the same code with different inputs.
http://docs.oracle.com/javase/tutorial/java/generics/why.html
![Page 3: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/3.jpg)
GENERICS FROM 10,000 FEET
• Benefits:
• Write less code (code re-use)
• They enable programmers to implement generic
algorithms once (less bugs)
• Stronger type checks at compile time.
• More errors are found at compile-time through static
checking
http://docs.oracle.com/javase/tutorial/java/generics/why.html
![Page 4: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/4.jpg)
GENERICS FROM 10,000 FEET
http://docs.oracle.com/javase/tutorial/java/generics/why.html
bytecode.jar
code.java:
class Foo{ /*does things */}
I DE
compiler distribution Java Virtual Machine great things
happen
![Page 5: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/5.jpg)
GENERICS FROM 10,000 FEET• Elimination of casts.
• The following code snippet without generics requires casting:
• When re-written to use generics, the code does not require
casting:
http://docs.oracle.com/javase/tutorial/java/generics/why.html
List list = new ArrayList();list.add("hello");String s = (String) list.get(0);
List<String> list = new ArrayList<String>();list.add("hello");String s = list.get(0); // no cast
![Page 6: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/6.jpg)
GENERICS FROM 10,000 FEET
Data Structures in Java, for the Principled Programmer
• Questions:
• What does the code on the left do?
• What is different about the code on the right?
• Is the code syntactically correct?
![Page 7: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/7.jpg)
GENERICS FROM 10,000 FEET
• Questions:
• What does the code on the left do?
• What is different about the code on the right?
• Is the code syntactically correct?
• Let’s try it
Data Structures in Java, for the Principled Programmer
![Page 8: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/8.jpg)
GENERICS FROM 10,000 FEET
http://docs.oracle.com/javase/tutorial/java/generics/why.html
bytecode.jar
code.java:
class Foo{ /*does things */}
I DE
compiler distribution Java Virtual Machine great things
happen
![Page 9: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/9.jpg)
GENERICS FROM 10,000 FEET
• Questions:
• What is different about the code with Generics?
Data Structures in Java, for the Principled Programmer
![Page 10: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/10.jpg)
GENERICS FROM 10,000 FEET
• Questions:
• What happened to the code on the right?
Data Structures in Java, for the Principled Programmer
![Page 11: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/11.jpg)
GENERICS FROM 10,000 FEET
http://docs.oracle.com/javase/tutorial/java/generics/why.html
bytecode.jar
code.java:
class Foo{ /*does things */}
I DE
compiler distribution Java Virtual Machine great things
happen
![Page 12: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/12.jpg)
GENERICS FROM 10,000 FEET
http://docs.oracle.com/javase/tutorial/java/generics/why.html
• Here’s what’s cool:
• When the developer made the Vector class they had no
idea that I was going to use it with Strings
• I could have used it with some other class
![Page 13: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/13.jpg)
![Page 14: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/14.jpg)
• Components of a method signature
• visibility
• return type
• name
• full name would include the class (and the package)
• Identity.setPassword
• parameter list
• parameter type
• parameter name
![Page 15: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/15.jpg)
• Generics are a small language within a language for
declaring types
• Let’s work through one
• We are clever hackers and we want to let people have first
and last names
![Page 16: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/16.jpg)
![Page 17: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/17.jpg)
• But we also know that we can use this for more than just
names
• Let’s abstract it into a general Pair
![Page 18: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/18.jpg)
![Page 19: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/19.jpg)
![Page 20: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/20.jpg)
• Mission Accomplished!
• Word has spread far and wide and now we’ve been hired
by an EMR company
• They want us to add BMI to our Identity class
• That’s a height and weight combo
• Hey! That sounds like another Pair!
• We’ve already done that right?
![Page 21: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/21.jpg)
![Page 22: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/22.jpg)
• Hmm…
• That’s easy enough to fix. Let’s just make another Pair
class…
![Page 23: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/23.jpg)
• Conceptually our Pair class is agnostic to what kind of type
gets used with it
• it doesn’t really matter
• But the Java language is tying our hands
• But we are stuck having to define a new class for every single
application that needs a different type
• Enter Generics
• Generics let you manipulate parameters without knowing their
type, but without losing static type checking in the compiler
![Page 24: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/24.jpg)
![Page 25: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/25.jpg)
![Page 26: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/26.jpg)
• Now, mission accomplished
• That’s 80% of everything to know about Generics
• It’s basically that easy, but there are some details that
you need to know
• The final 20%….
![Page 27: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/27.jpg)
• You can’t plug in a primitive type to a Generic
• You can’t use a Generic in an Array (it’s primitive)
Generics and primitive types don’t play well together
![Page 28: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/28.jpg)
• When writing a Generic you actually have a little more
control over how a developer uses it
Sometimes you care just a little bit about the type
![Page 29: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/29.jpg)
• How could you make a Quad?
Generics can be extended
![Page 30: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/30.jpg)
• You cannot inherit just a portion of a type
Generic Types are Atomic
Object someObject = new Object();Integer someInteger = new Integer(10);someObject = someInteger; // OK
http://docs.oracle.com/javase/tutorial/java/generics/inheritance.html
public void someMethod(Number n) { /* ... */ }
someMethod(new Integer(10)); // OKsomeMethod(new Double(10.1)); // OK
Integer foo = new Integer(10);Double bar = new Double(10.1);PairGeneric<Number,Number> p= new PairGeneric<Number,Number>(foo,bar);
![Page 31: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/31.jpg)
• You cannot inherit just a portion of a type
Generic Types are Atomic
http://docs.oracle.com/javase/tutorial/java/generics/inheritance.html
public Double sum(Vector<Number> v) {double running = 0.0;for(int i = 0 ; i < v.size(); i++){
running += v.get(i).doubleValue();}return running;
}
Number
Double
Object
Vector<Number>
Vector<Double>
Vector<Object>Object
Vector<Number> Vector<Double>
![Page 32: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/32.jpg)
Work it out
• Will the following code compile?
public class Algorithm{ public T max(T x, T y) { return x > y ? x : y; }}
![Page 33: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/33.jpg)
Generics from 10,000 feet
• Generics enable types (classes and interfaces) to be
parameterized.
• The input to Generics are types
• The output of Generics are new types
• They are like parameters in methods
• but they are about types, not data, not values
• they are meta-data
• They let you re-use the same code with different inputs.
http://docs.oracle.com/javase/tutorial/java/generics/why.html
![Page 34: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/34.jpg)
Generics from 10,000 feet
• Benefits:
• Write less code (code re-use)
• Enabling programmers to implement generic
algorithms.
• Stronger type checks at compile time.
• More errors are found at compile-time through static
checking
http://docs.oracle.com/javase/tutorial/java/generics/why.html
![Page 35: GENERICS IN JAVA - Westmont Collegedjp3.westmont.edu/classes/2016_01_CS030/Lectures/Lecture...GENERICS FROM 10,000 FEET • Benefits: • Write less code (code re-use) • They enable](https://reader035.vdocuments.us/reader035/viewer/2022071301/60a16a7782f9a5003e5df9cc/html5/thumbnails/35.jpg)