SlideShare une entreprise Scribd logo
1  sur  8
Generics in JAVA
-Jai Marathe
Introduction
• Generics implement parametric polymorphism
– Parametric: The type parameter (e.g., <T>)…
– Polymorphism: …can take many forms
• However, if we’re going to program with
parameterized types, we need to understand how the
language rules apply to them
• Java generics are implemented using type
erasure, which leads to all sorts of wacky issues, as
we’ll see
Wildcard Types
• So, what is List<Apple> a subtype of?
• The supertype of all kinds of lists is List<?>, the List
of unknown
• The ? is a wildcard that matches anything
• We can’t add things (except null) to a List<?>, since
we don’t know what the List is really of
• But we can retrieve things and treat them as
• Objects, since we know they are at least that
Josh Bloch’s Bounded Wildcards
Rule
• Use <? extends T> when parameterized instance is a
T producer (for reading/input)
• Use <? super T> when parameterized instance is a T
consumer (for writing/output)
Generic Methods
• You can parameterize methods too. Here’s a signature
from the Java API:
• static <T> void fill(List<? super T> list, T obj);
• Easy enough, yeah? Try this one on for size: static <T
extends Object & Comparable<? super T>> T
max(Collection<? extends T> coll);
• You don’t need to explicitly instantiate generic
methods (the compiler will figure it out)
How Generics are Implemented
• Rather than change every JVM between Java 1.4 and
1.5, they chose to use erasure
• After the compiler does its type checking, it discards
the generics; the JVM never sees them!
• It works something like this:
– Type information between angle brackets is thrown
• out, e.g., List<String> -> List
– Uses of type variables are replaced by their upper
bound (usually Object)
– Casts are inserted to preserve type‐correctness
Bounded Wildcards
• Wildcard types can have upper and lower
bounds
• A List<? extends Fruit> is a List of items that
have unknown type but are all at least Fruits
– So it can contain Fruits and Apples but
not Peas
• A List<? super Fruit> is a List of items that
have unknown type but are all at most Fruits
– So it can contain Fruits and Objects but not Apples
Thank You…!!!

Contenu connexe

Tendances (16)

Python Data Types
Python Data TypesPython Data Types
Python Data Types
 
Type system
Type systemType system
Type system
 
Clean Code
Clean CodeClean Code
Clean Code
 
List and iterator
List and iteratorList and iterator
List and iterator
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Presentation on python data type
Presentation on python data typePresentation on python data type
Presentation on python data type
 
I want a pony
I want a ponyI want a pony
I want a pony
 
Lecture02 java
Lecture02 javaLecture02 java
Lecture02 java
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variables
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 
Data types IN JAVA
Data types IN JAVAData types IN JAVA
Data types IN JAVA
 
Parametricity
ParametricityParametricity
Parametricity
 
More oop in java
More oop in javaMore oop in java
More oop in java
 
Java generics(Under The Hood Of The Compiler) by Harmeet singh
Java generics(Under The Hood Of The Compiler) by Harmeet singhJava generics(Under The Hood Of The Compiler) by Harmeet singh
Java generics(Under The Hood Of The Compiler) by Harmeet singh
 
Java Program Structure
Java Program StructureJava Program Structure
Java Program Structure
 
2CPP12 - Method Overriding
2CPP12 - Method Overriding2CPP12 - Method Overriding
2CPP12 - Method Overriding
 

En vedette

On Parameterised Types and Java Generics
On Parameterised Types and Java GenericsOn Parameterised Types and Java Generics
On Parameterised Types and Java GenericsYann-Gaël Guéhéneuc
 
Enterprise Tic-Tac-Toe
Enterprise Tic-Tac-ToeEnterprise Tic-Tac-Toe
Enterprise Tic-Tac-ToeScott Wlaschin
 
Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)Scott Wlaschin
 
Expressjs basic to advance, power by Node.js
Expressjs basic to advance, power by Node.jsExpressjs basic to advance, power by Node.js
Expressjs basic to advance, power by Node.jsCaesar Chi
 

En vedette (7)

2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
 
Use the @types, Luke
Use the @types, LukeUse the @types, Luke
Use the @types, Luke
 
On Parameterised Types and Java Generics
On Parameterised Types and Java GenericsOn Parameterised Types and Java Generics
On Parameterised Types and Java Generics
 
Enterprise Tic-Tac-Toe
Enterprise Tic-Tac-ToeEnterprise Tic-Tac-Toe
Enterprise Tic-Tac-Toe
 
Pizza compiler
Pizza compilerPizza compiler
Pizza compiler
 
Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)
 
Expressjs basic to advance, power by Node.js
Expressjs basic to advance, power by Node.jsExpressjs basic to advance, power by Node.js
Expressjs basic to advance, power by Node.js
 

Similaire à Generics of JAVA

python presentation.pptx
python presentation.pptxpython presentation.pptx
python presentation.pptxNightTune44
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_reviewEdureka!
 
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdfProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdflailoesakhan
 
Java Generics.ppt
Java Generics.pptJava Generics.ppt
Java Generics.pptbrayazar
 
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdfProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdflailoesakhan
 
Computer Sience, This is my presentation for beginners coding in python
Computer Sience, This is my presentation for beginners coding in pythonComputer Sience, This is my presentation for beginners coding in python
Computer Sience, This is my presentation for beginners coding in pythonalin173596
 
Learning core java
Learning core javaLearning core java
Learning core javaAbhay Bharti
 
Beginning Java for .NET developers
Beginning Java for .NET developersBeginning Java for .NET developers
Beginning Java for .NET developersAndrei Rinea
 
MODULE-2.pptx
MODULE-2.pptxMODULE-2.pptx
MODULE-2.pptxASRPANDEY
 

Similaire à Generics of JAVA (20)

python presentation.pptx
python presentation.pptxpython presentation.pptx
python presentation.pptx
 
basics dart.pdf
basics dart.pdfbasics dart.pdf
basics dart.pdf
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_review
 
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdfProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
 
Java Generics.ppt
Java Generics.pptJava Generics.ppt
Java Generics.ppt
 
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdfProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
 
Python Data-Types
Python Data-TypesPython Data-Types
Python Data-Types
 
Computer Sience, This is my presentation for beginners coding in python
Computer Sience, This is my presentation for beginners coding in pythonComputer Sience, This is my presentation for beginners coding in python
Computer Sience, This is my presentation for beginners coding in python
 
Typescript
TypescriptTypescript
Typescript
 
Ruby basics
Ruby basicsRuby basics
Ruby basics
 
Tuples.pptx
Tuples.pptxTuples.pptx
Tuples.pptx
 
Learning core java
Learning core javaLearning core java
Learning core java
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
 
unit1 python.pptx
unit1 python.pptxunit1 python.pptx
unit1 python.pptx
 
Python Demo.pptx
Python Demo.pptxPython Demo.pptx
Python Demo.pptx
 
Beginning Java for .NET developers
Beginning Java for .NET developersBeginning Java for .NET developers
Beginning Java for .NET developers
 
MODULE-2.pptx
MODULE-2.pptxMODULE-2.pptx
MODULE-2.pptx
 
Python_basics.pptx
Python_basics.pptxPython_basics.pptx
Python_basics.pptx
 
Python Demo.pptx
Python Demo.pptxPython Demo.pptx
Python Demo.pptx
 

Generics of JAVA

  • 2. Introduction • Generics implement parametric polymorphism – Parametric: The type parameter (e.g., <T>)… – Polymorphism: …can take many forms • However, if we’re going to program with parameterized types, we need to understand how the language rules apply to them • Java generics are implemented using type erasure, which leads to all sorts of wacky issues, as we’ll see
  • 3. Wildcard Types • So, what is List<Apple> a subtype of? • The supertype of all kinds of lists is List<?>, the List of unknown • The ? is a wildcard that matches anything • We can’t add things (except null) to a List<?>, since we don’t know what the List is really of • But we can retrieve things and treat them as • Objects, since we know they are at least that
  • 4. Josh Bloch’s Bounded Wildcards Rule • Use <? extends T> when parameterized instance is a T producer (for reading/input) • Use <? super T> when parameterized instance is a T consumer (for writing/output)
  • 5. Generic Methods • You can parameterize methods too. Here’s a signature from the Java API: • static <T> void fill(List<? super T> list, T obj); • Easy enough, yeah? Try this one on for size: static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll); • You don’t need to explicitly instantiate generic methods (the compiler will figure it out)
  • 6. How Generics are Implemented • Rather than change every JVM between Java 1.4 and 1.5, they chose to use erasure • After the compiler does its type checking, it discards the generics; the JVM never sees them! • It works something like this: – Type information between angle brackets is thrown • out, e.g., List<String> -> List – Uses of type variables are replaced by their upper bound (usually Object) – Casts are inserted to preserve type‐correctness
  • 7. Bounded Wildcards • Wildcard types can have upper and lower bounds • A List<? extends Fruit> is a List of items that have unknown type but are all at least Fruits – So it can contain Fruits and Apples but not Peas • A List<? super Fruit> is a List of items that have unknown type but are all at most Fruits – So it can contain Fruits and Objects but not Apples