Professional Documents
Culture Documents
Java Generics Cs580c Fall 2007
Java Generics Cs580c Fall 2007
Architectural Differences
Summary
Michael R. Head
Introduction
Architectural Differences
Summary
Outline
Introduction
Generic Programming in General
Generic Programming in C++
Generic Programming in Java
Architectural Differences
Type Erasure
Parameterized Type Bounds
Metaprogramming
Michael R. Head
Introduction
Architectural Differences
Summary
Outline
Introduction
Generic Programming in General
Generic Programming in C++
Generic Programming in Java
Architectural Differences
Type Erasure
Parameterized Type Bounds
Metaprogramming
Michael R. Head
Introduction
Architectural Differences
Summary
Michael R. Head
Introduction
Architectural Differences
Summary
Generic algorithms
iteration, std::max()
Michael R. Head
Introduction
Architectural Differences
Summary
Outline
Introduction
Generic Programming in General
Generic Programming in C++
Generic Programming in Java
Architectural Differences
Type Erasure
Parameterized Type Bounds
Metaprogramming
Michael R. Head
Introduction
Architectural Differences
Summary
Introduction
Architectural Differences
Summary
Introduction
Architectural Differences
Summary
Outline
Introduction
Generic Programming in General
Generic Programming in C++
Generic Programming in Java
Architectural Differences
Type Erasure
Parameterized Type Bounds
Metaprogramming
Michael R. Head
Introduction
Architectural Differences
Summary
Michael R. Head
Introduction
Architectural Differences
Summary
Michael R. Head
Introduction
Architectural Differences
Summary
Type Erasure
Parameterized Type Bounds
Metaprogramming
Outline
Introduction
Generic Programming in General
Generic Programming in C++
Generic Programming in Java
Architectural Differences
Type Erasure
Parameterized Type Bounds
Metaprogramming
Michael R. Head
Introduction
Architectural Differences
Summary
Type Erasure
Parameterized Type Bounds
Metaprogramming
Type Erasure
Michael R. Head
Introduction
Architectural Differences
Summary
Type Erasure
Parameterized Type Bounds
Metaprogramming
Type Erasure
Michael R. Head
Introduction
Architectural Differences
Summary
Type Erasure
Parameterized Type Bounds
Metaprogramming
Type Erasure
Michael R. Head
Introduction
Architectural Differences
Summary
Type Erasure
Parameterized Type Bounds
Metaprogramming
What is the erased type of each of these? Hint: The first one is
Object
<T>
<T extends Number>
<T extends Comparable<T>>
<T extends Cloneable & Comparable<T>>
<T extends Object & Comparable<T>>
<S, T extends S>
Michael R. Head
Introduction
Architectural Differences
Summary
Type Erasure
Parameterized Type Bounds
Metaprogramming
Michael R. Head
Object
Number
Comparable
Cloneable
Object
Object,Object
Introduction
Architectural Differences
Summary
Type Erasure
Parameterized Type Bounds
Metaprogramming
Implications
No new classes are created when instantiating a generic
type
Instantiating a generic class has no runtime overhead over
using an equivalent class that uses Object rather than X
Indeed, the two classes would compile to almost identical
byte code
Also, no potential performance benefit from using a
specially compiled version (say for ints to eliminate
autoboxing)
Introduction
Architectural Differences
Summary
Type Erasure
Parameterized Type Bounds
Metaprogramming
Outline
Introduction
Generic Programming in General
Generic Programming in C++
Generic Programming in Java
Architectural Differences
Type Erasure
Parameterized Type Bounds
Metaprogramming
Michael R. Head
Introduction
Architectural Differences
Summary
Type Erasure
Parameterized Type Bounds
Metaprogramming
Michael R. Head
Introduction
Architectural Differences
Summary
Type Erasure
Parameterized Type Bounds
Metaprogramming
class
class
class
class
class
class
class
class
class
class
class
X0 <T
X1 <T
X2 <T
X3 <T
X4 <T
X5 <T
X6 <T
X7 <T
X8 <T
X9 <T
X10<T
extends
extends
extends
extends
extends
extends
extends
extends
extends
extends
extends
int> { ... }
Object[]> { ... }
Number> { ... }
String> { ... }
Runnable> { ... }
Thread.State> { ... }
List> { ... }
List<String>> { ... }
List<? extends Number>> { ... }
Comparable<? super Number>> { ... }
Map.Entry<?,?>> { ... }
Michael R. Head
Introduction
Architectural Differences
Summary
Type Erasure
Parameterized Type Bounds
Metaprogramming
X2 <T
X3 <T
X4 <T
X5 <T
X6 <T
X7 <T
X8 <T
X9 <T
X10<T
extends
extends
extends
extends
extends
extends
extends
extends
extends
Number> { ... }
String> { ... }
Runnable> { ... }
Thread.State> { ... }
List> { ... }
List<String>> { ... }
List<? extends Number>> { ... }
Comparable<? super Number>> { ... }
Map.Entry<?,?>> { ... }
Michael R. Head
Introduction
Architectural Differences
Summary
Type Erasure
Parameterized Type Bounds
Metaprogramming
Implications
Michael R. Head
Introduction
Architectural Differences
Summary
Type Erasure
Parameterized Type Bounds
Metaprogramming
Outline
Introduction
Generic Programming in General
Generic Programming in C++
Generic Programming in Java
Architectural Differences
Type Erasure
Parameterized Type Bounds
Metaprogramming
Michael R. Head
Introduction
Architectural Differences
Summary
Type Erasure
Parameterized Type Bounds
Metaprogramming
Template Metaprogramming
Michael R. Head
Introduction
Architectural Differences
Summary
Type Erasure
Parameterized Type Bounds
Metaprogramming
Implications
Abusing your compiler for extremely early binding
Can do really powerful computations at compile time
Compute constants such as to any precision required
Let the compiler generate lookup tables (for example, for
base64 codecs)
Loop unrolling
Michael R. Head
Introduction
Architectural Differences
Summary
Summary
Resources
Summary
Michael R. Head
Introduction
Architectural Differences
Summary
Summary
Resources
Resources
On Java Generics...
Using and Programming Generics in J2SE 5.0
Java Generics FAQs
On C++ Templates...
C++ Templates FAQ
A gentle introduction to Template Metaprogramming with
C++
C++ FAQ Lite: Templates
Other Comparisons...
Bruce Eckel on Templates vs. Generics
Generics in C#, Java, and C++ (A Conversation with
Anders Hejlsberg)
Michael R. Head