SlideShare a Scribd company logo
Java/J2EE Programming Training
Java Generics
Page 1Classification: Restricted
Agenda
• background and goals of generic programming
• basics of generic classes = parameterized types
• generic methods for general algorithms
• inheritance rules for generic types
• bounded type parameters
• generic code and the Java Virtual Machine
• restrictions and limitations
• wildcard types and wildcard type capture
Page 2Classification: Restricted
2
Why generic programming
Background
• old version 1.4 Java collections were Object-based and required the use of
ugly casts
• cannot specify the exact type of elements
• must cast to specific classes when accessing
Java generics
• lets you write code that is safer and easier to read
• is especially useful for general data structures, such as ArrayList
• generic programming = programming with classes and methods
parameterized with types
Page 3Classification: Restricted
3
Why generic programming (cont.)
• generic types are a powerful tool to write reusable object-oriented
components and libraries
• however, the generic language features are not easy to master and can be
misused
• their full understanding requires the knowledge of the type theory of
programming languages
• especially covariant and contravariant typing
• the following introduces the main aspects of Java generics and their use and
limitations
• we mostly inspect illustrative samples of what is and what is not allowed, with
some short glimpses inside the JVM implementation
Page 4Classification: Restricted
4
Why generic programming (cont.)
Java generics
• in principle, supports statically-typed data structures
• early detection of type violations
• cannot insert a string into ArrayList <Number>
• also, hides automatically generated casts
• superficially resembles C++ templates
• C++ templates are factories for ordinary classes and functions
• a new class is always instantiated for given distinct generic parameters
(type or other)
• in Java, generic types are factories for compile-time entities related to types
and methods
Page 5Classification: Restricted
5
Definition of a simple generic class
class Pair <T> {
public T first;
public T second;
public Pair (T f, T s) { first = f; second = s; }
public Pair () { first = null; second = null; }
}
• you instantiate the generic class by substituting actual types for type variables,
as: Pair <String>
• you can think the result as a class with a constructor
public Pair (String f, String s), etc . .
• you can then use the instantiated generic class as it were a normal class
(almost):
Pair <String> pair = new Pair <String> ("1","2");
Page 6Classification: Restricted
6
Multiple type parameters allowed
• you can have multiple type parameters
class Pair <T, U> {
public T first;
public U second;
public Pair (T x, U y) { first = x; second = y; }
public Pair () { first = null; second = null; }
}
• to instantiate: Pair <String, Number>
Page 7Classification: Restricted
7
Generic static algorithms
• you can define generic methods both inside ordinary classes and inside
generic classes
class Algorithms { // some utility class
public static <T> T getMiddle (T [ ] a) {
return a [ a.length / 2 ];
}
. . .
}
• when calling a generic method, you can specify type
String s = Algorithms.<String>getMiddle (names);
• but in most cases, the compiler infers the type:
String s = Algorithms. getMiddle (names);
Page 8Classification: Restricted
8
Inheritance rules for generic types
Page 9Classification: Restricted
9
Comments on inheritance relations
• Pair<Manager> matches Pair<? extends Employee> => subtype relation
(covariant typing)
• Pair<Object> matches Pair<? super Employee>
=> subtype relation (contravariant typing)
• Pair<Employee> can contain only Employees, but
Pair<Object> may be assigned anything (Numbers)
=> no subtype relation
• also: Pair<T> <= Pair<?> <= Pair (raw)
List <String> sl = new LinkedList <String> ();
List x = sl; // OK
x.add (new Integer (5)); // type safety warning
. .
String str = sl.get (0); // throws ClassCast.
Page 10Classification: Restricted
10
Bounds for type variables
• consider the min algorithm: find the smallest item in a given array of elements
• to compile this, must restrict T to implement the Comparable interface that
provides compareTo
public static <T extends Comparable>
T min (T [ ] a) { // this is almost correct
if (a.length == 0) throw new InvalidArg.. (..);
T smallest = a [0];
for (int i = 1; i < a.length; i++)
if (smallest.compareTo (a [i]) > 0) // T constraint
smallest = a [i];
return smallest;
}
Page 11Classification: Restricted
11
Bounds for type variables (cont.)
• however, Comparable is itself a generic interface
• moreover, any supertype of T may have extended it
public static <T extends Object & // bounding class
Comparable <? super T>>
T min (T [ ] a) { . . . // the more general form
T smallest = a [0];
for (int i = 1; i < a.length; i++)
if (smallest.compareTo (a [i]) > 0) // T constraint
smallest = a [i];
return smallest;
}
• cannot inherit multiple different instantiations of the same generic type (class
or interface)
• an inherited generic type is fixed for subtypes, too
Page 12Classification: Restricted
12
Generic code and the JVM
• the JVM has no instantiations of generic types
• a generic type definition is compiled once only, and
a corresponding raw type is produced
• the name of the raw type is the same name but type variables removed
• type variables are erased and replaced by their bounding types (or Object if no
bounds); e.g.:
class Pair { // the raw type for Pair <T>
public Object first;
public Object second;
public Pair (Object f, Object s) { . . }
}
• byte code has some generic info, but objects don't
Page 13Classification: Restricted
13
Generic code and the JVM (cont.)
• Pair <String> and Pair <Employee> use the same bytecode generated as the
raw class Pair
• when translating generic expressions, such as
Pair <Employee> buddies = new Pair < . .;
Employee buddy = buddies.first;
• the compiler uses the raw class and automatically inserts a cast from Object to
Employee:
Employee buddy = (Employee)buddies.first;
• in C++, no such casts are required since class instantiations already use
specific types
• if multiple constraints (Object & Comparable. .) then the type parameter is
replaced by the first one
Page 14Classification: Restricted
14
Overriding of methods of generic type
• consider a generic class with a non-final method:
class Pair <T> { // parameter T is erased from code
public void setSecond (T s) { second = s; } . .
• to override such type-erased methods, the compiler must generate extra
bridge methods:
class DateInterval extends Pair <Date> {
public void setSecond (Date high) { // override
if (high.compareTo (first) < 0) throw new . .
second = high; // otherwise OK
}
public void setSecond (Object s) { // bridge method
setSecond ((Date)s); // generated by compiler
} . .
Page 15Classification: Restricted
15
Restrictions and limitations
• in Java, generic types are compile-time entities
• in C++, instantiations of a class template are compiled separately as source
code, and tailored code is produced for each one
• primitive type parameters (Pair <int>) not allowed
• in C++, both classes and primitive types allowed
• objects in JVM have non-generic classes:
Pair<String> strPair = new Pair<String> . .;
Pair<Number> numPair = new Pair<Number> . .;
b = strPair.getClass () == numPair.getClass ();
assert b == true; // both of the raw class Pair
• but byte-code has reflective info about generics
Page 16Classification: Restricted
16
Restrictions and limitations (cont.)
• instantiations of generic parameter T are not allowed
new T () // ERROR: whatever T to produce?
new T [10]
• arrays of parameterized types are not allowed
new Pair <String> [10]; // ERROR
• since type erasure removes type information needed for checks of array
assignments
• static fields and static methods with type parameters are not allowed
class Singleton <T> {
private static T singleOne; // ERROR
• since after type erasure, one class and one shared static field for all
instantiations and their objects
Page 17Classification: Restricted
17
Wildcard types
• note that the raw class Pair is not equal Pair <?>
Pair pair1 = . .;
pair1.first = new Double (10.0); // WARNING
Pair <?> pair2 = . .;
pair2.first = new Double (10.0); // ERROR
• but some operations have no type constraints:
public static boolean hasNulls (Pair <?> p) {
return p.first == null || p.second == null;
}
• alternatively, you could provide a generic method
public static <T> boolean hasNulls (Pair <T> p)
• generally, prefer wildcard types (but use generic method with type T when
multiple parameters)
Page 18Classification: Restricted
18
Wildcard capture
• the wildcard type ? cannot be used as a declared type of any variables (as
in the previous slide)
Pair <?> p = new Pair <String> ("one", "two"); . .
p.first = p.second; // ERROR: unknown type
• but, can sometimes use a generic method to capture the wildcard:
public static <T> void rotate (Pair <T> p) {
T temp = p.first; p.first = p.second;
p.second = temp;
}
• the compile checks that such a capture is legal
• e.g., the context ensures that T is unambiguous
Page 19Classification: Restricted
19
Collections and algorithms
• goal: design a minimal interface that you need
• e.g., for max, implement to take any Collection
public static <T extends Object &
Comparable <? super T>>
T max (Collection <? extends T> c) {
// a hypothetical implementation:
Iterator <T> it = c.iterator ();
T largest = it.next (); // or throws NoSuchElement
while (it.hasNext ()) {
T val = it.next ();
if (largest.compareTo (val) < 0) largest = val;
}
return largest;
}
Page 20Classification: Restricted
Thank You
Ad

More Related Content

What's hot (20)

11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objects
maznabili
 
Java generics
Java genericsJava generics
Java generics
Hosein Zare
 
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptLecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops concept
manish kumar
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
Tareq Hasan
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritance
manish kumar
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
DevaKumari Vijay
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
Ramrao Desai
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
mcollison
 
Java basic
Java basicJava basic
Java basic
Sonam Sharma
 
Chapter 8 - Exceptions and Assertions Edit summary
Chapter 8 - Exceptions and Assertions  Edit summaryChapter 8 - Exceptions and Assertions  Edit summary
Chapter 8 - Exceptions and Assertions Edit summary
Eduardo Bergavera
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threads
DevaKumari Vijay
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
Eduardo Bergavera
 
Lecture 7 arrays
Lecture   7 arraysLecture   7 arrays
Lecture 7 arrays
manish kumar
 
Object and class
Object and classObject and class
Object and class
mohit tripathi
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
Praveen M Jigajinni
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
Lorna Mitchell
 
Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++
Hoang Nguyen
 
Core java concepts
Core java concepts Core java concepts
Core java concepts
javeed_mhd
 
Chapter 2 - Getting Started with Java
Chapter 2 - Getting Started with JavaChapter 2 - Getting Started with Java
Chapter 2 - Getting Started with Java
Eduardo Bergavera
 
11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objects
maznabili
 
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptLecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops concept
manish kumar
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
Tareq Hasan
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritance
manish kumar
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
DevaKumari Vijay
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
Ramrao Desai
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
mcollison
 
Chapter 8 - Exceptions and Assertions Edit summary
Chapter 8 - Exceptions and Assertions  Edit summaryChapter 8 - Exceptions and Assertions  Edit summary
Chapter 8 - Exceptions and Assertions Edit summary
Eduardo Bergavera
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threads
DevaKumari Vijay
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
Eduardo Bergavera
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
Lorna Mitchell
 
Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++
Hoang Nguyen
 
Core java concepts
Core java concepts Core java concepts
Core java concepts
javeed_mhd
 
Chapter 2 - Getting Started with Java
Chapter 2 - Getting Started with JavaChapter 2 - Getting Started with Java
Chapter 2 - Getting Started with Java
Eduardo Bergavera
 

Similar to Java Generics (20)

Java Generics.ppt
Java Generics.pptJava Generics.ppt
Java Generics.ppt
brayazar
 
Java generics final
Java generics finalJava generics final
Java generics final
Akshay Chaudhari
 
Generics Module 2Generics Module Generics Module 2.pptx
Generics Module 2Generics Module Generics Module 2.pptxGenerics Module 2Generics Module Generics Module 2.pptx
Generics Module 2Generics Module Generics Module 2.pptx
AlvasCSE
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
Connex
 
java training faridabad
java training faridabadjava training faridabad
java training faridabad
Woxa Technologies
 
Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)
Andrew Petryk
 
Generics
GenericsGenerics
Generics
Shahjahan Samoon
 
Java Tutorials
Java Tutorials Java Tutorials
Java Tutorials
Woxa Technologies
 
core java
core javacore java
core java
Vinodh Kumar
 
Generics Module 2Generics ModuleGenerics Module 2
Generics Module 2Generics ModuleGenerics Module 2Generics Module 2Generics ModuleGenerics Module 2
Generics Module 2Generics ModuleGenerics Module 2
AlvasCSE
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developement
frwebhelp
 
Csharp_mahesh
Csharp_maheshCsharp_mahesh
Csharp_mahesh
Ananthu Mahesh
 
Core Java Concepts
Core Java ConceptsCore Java Concepts
Core Java Concepts
mdfkhan625
 
Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)
David McCarter
 
Generics
GenericsGenerics
Generics
Kongu Engineering College, Perundurai, Erode
 
Java New Programming Features
Java New Programming FeaturesJava New Programming Features
Java New Programming Features
tarun308
 
Java For Automation
Java   For AutomationJava   For Automation
Java For Automation
Abhijeet Dubey
 
21CS642 Module 2 Generics PPT.pptx VI SEM CSE
21CS642 Module 2 Generics PPT.pptx VI SEM CSE21CS642 Module 2 Generics PPT.pptx VI SEM CSE
21CS642 Module 2 Generics PPT.pptx VI SEM CSE
VENKATESHBHAT25
 
ESINF02-JCF.pdfESINF02-JCF.pdfESINF02-JCF.pdf
ESINF02-JCF.pdfESINF02-JCF.pdfESINF02-JCF.pdfESINF02-JCF.pdfESINF02-JCF.pdfESINF02-JCF.pdf
ESINF02-JCF.pdfESINF02-JCF.pdfESINF02-JCF.pdf
LusArajo20
 
Java
Java Java
Java
Prabhat gangwar
 
Java Generics.ppt
Java Generics.pptJava Generics.ppt
Java Generics.ppt
brayazar
 
Generics Module 2Generics Module Generics Module 2.pptx
Generics Module 2Generics Module Generics Module 2.pptxGenerics Module 2Generics Module Generics Module 2.pptx
Generics Module 2Generics Module Generics Module 2.pptx
AlvasCSE
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
Connex
 
Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)
Andrew Petryk
 
Generics Module 2Generics ModuleGenerics Module 2
Generics Module 2Generics ModuleGenerics Module 2Generics Module 2Generics ModuleGenerics Module 2
Generics Module 2Generics ModuleGenerics Module 2
AlvasCSE
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developement
frwebhelp
 
Core Java Concepts
Core Java ConceptsCore Java Concepts
Core Java Concepts
mdfkhan625
 
Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)
David McCarter
 
Java New Programming Features
Java New Programming FeaturesJava New Programming Features
Java New Programming Features
tarun308
 
21CS642 Module 2 Generics PPT.pptx VI SEM CSE
21CS642 Module 2 Generics PPT.pptx VI SEM CSE21CS642 Module 2 Generics PPT.pptx VI SEM CSE
21CS642 Module 2 Generics PPT.pptx VI SEM CSE
VENKATESHBHAT25
 
ESINF02-JCF.pdfESINF02-JCF.pdfESINF02-JCF.pdf
ESINF02-JCF.pdfESINF02-JCF.pdfESINF02-JCF.pdfESINF02-JCF.pdfESINF02-JCF.pdfESINF02-JCF.pdf
ESINF02-JCF.pdfESINF02-JCF.pdfESINF02-JCF.pdf
LusArajo20
 
Ad

More from DeeptiJava (13)

Generating the Server Response: HTTP Status Codes
Generating the Server Response: HTTP Status CodesGenerating the Server Response: HTTP Status Codes
Generating the Server Response: HTTP Status Codes
DeeptiJava
 
Java Collection
Java CollectionJava Collection
Java Collection
DeeptiJava
 
Java Exception Handling
Java Exception HandlingJava Exception Handling
Java Exception Handling
DeeptiJava
 
Java OOPs
Java OOPs Java OOPs
Java OOPs
DeeptiJava
 
Java Access Specifier
Java Access SpecifierJava Access Specifier
Java Access Specifier
DeeptiJava
 
Java JDBC
Java JDBCJava JDBC
Java JDBC
DeeptiJava
 
Java Thread
Java ThreadJava Thread
Java Thread
DeeptiJava
 
Java Inner Class
Java Inner ClassJava Inner Class
Java Inner Class
DeeptiJava
 
JSP Part 2
JSP Part 2JSP Part 2
JSP Part 2
DeeptiJava
 
JSP Part 1
JSP Part 1JSP Part 1
JSP Part 1
DeeptiJava
 
Java I/O
Java I/OJava I/O
Java I/O
DeeptiJava
 
Java Hibernate Basics
Java Hibernate BasicsJava Hibernate Basics
Java Hibernate Basics
DeeptiJava
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
DeeptiJava
 
Generating the Server Response: HTTP Status Codes
Generating the Server Response: HTTP Status CodesGenerating the Server Response: HTTP Status Codes
Generating the Server Response: HTTP Status Codes
DeeptiJava
 
Java Collection
Java CollectionJava Collection
Java Collection
DeeptiJava
 
Java Exception Handling
Java Exception HandlingJava Exception Handling
Java Exception Handling
DeeptiJava
 
Java Access Specifier
Java Access SpecifierJava Access Specifier
Java Access Specifier
DeeptiJava
 
Java Inner Class
Java Inner ClassJava Inner Class
Java Inner Class
DeeptiJava
 
Java Hibernate Basics
Java Hibernate BasicsJava Hibernate Basics
Java Hibernate Basics
DeeptiJava
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
DeeptiJava
 
Ad

Recently uploaded (20)

Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 

Java Generics

  • 2. Page 1Classification: Restricted Agenda • background and goals of generic programming • basics of generic classes = parameterized types • generic methods for general algorithms • inheritance rules for generic types • bounded type parameters • generic code and the Java Virtual Machine • restrictions and limitations • wildcard types and wildcard type capture
  • 3. Page 2Classification: Restricted 2 Why generic programming Background • old version 1.4 Java collections were Object-based and required the use of ugly casts • cannot specify the exact type of elements • must cast to specific classes when accessing Java generics • lets you write code that is safer and easier to read • is especially useful for general data structures, such as ArrayList • generic programming = programming with classes and methods parameterized with types
  • 4. Page 3Classification: Restricted 3 Why generic programming (cont.) • generic types are a powerful tool to write reusable object-oriented components and libraries • however, the generic language features are not easy to master and can be misused • their full understanding requires the knowledge of the type theory of programming languages • especially covariant and contravariant typing • the following introduces the main aspects of Java generics and their use and limitations • we mostly inspect illustrative samples of what is and what is not allowed, with some short glimpses inside the JVM implementation
  • 5. Page 4Classification: Restricted 4 Why generic programming (cont.) Java generics • in principle, supports statically-typed data structures • early detection of type violations • cannot insert a string into ArrayList <Number> • also, hides automatically generated casts • superficially resembles C++ templates • C++ templates are factories for ordinary classes and functions • a new class is always instantiated for given distinct generic parameters (type or other) • in Java, generic types are factories for compile-time entities related to types and methods
  • 6. Page 5Classification: Restricted 5 Definition of a simple generic class class Pair <T> { public T first; public T second; public Pair (T f, T s) { first = f; second = s; } public Pair () { first = null; second = null; } } • you instantiate the generic class by substituting actual types for type variables, as: Pair <String> • you can think the result as a class with a constructor public Pair (String f, String s), etc . . • you can then use the instantiated generic class as it were a normal class (almost): Pair <String> pair = new Pair <String> ("1","2");
  • 7. Page 6Classification: Restricted 6 Multiple type parameters allowed • you can have multiple type parameters class Pair <T, U> { public T first; public U second; public Pair (T x, U y) { first = x; second = y; } public Pair () { first = null; second = null; } } • to instantiate: Pair <String, Number>
  • 8. Page 7Classification: Restricted 7 Generic static algorithms • you can define generic methods both inside ordinary classes and inside generic classes class Algorithms { // some utility class public static <T> T getMiddle (T [ ] a) { return a [ a.length / 2 ]; } . . . } • when calling a generic method, you can specify type String s = Algorithms.<String>getMiddle (names); • but in most cases, the compiler infers the type: String s = Algorithms. getMiddle (names);
  • 10. Page 9Classification: Restricted 9 Comments on inheritance relations • Pair<Manager> matches Pair<? extends Employee> => subtype relation (covariant typing) • Pair<Object> matches Pair<? super Employee> => subtype relation (contravariant typing) • Pair<Employee> can contain only Employees, but Pair<Object> may be assigned anything (Numbers) => no subtype relation • also: Pair<T> <= Pair<?> <= Pair (raw) List <String> sl = new LinkedList <String> (); List x = sl; // OK x.add (new Integer (5)); // type safety warning . . String str = sl.get (0); // throws ClassCast.
  • 11. Page 10Classification: Restricted 10 Bounds for type variables • consider the min algorithm: find the smallest item in a given array of elements • to compile this, must restrict T to implement the Comparable interface that provides compareTo public static <T extends Comparable> T min (T [ ] a) { // this is almost correct if (a.length == 0) throw new InvalidArg.. (..); T smallest = a [0]; for (int i = 1; i < a.length; i++) if (smallest.compareTo (a [i]) > 0) // T constraint smallest = a [i]; return smallest; }
  • 12. Page 11Classification: Restricted 11 Bounds for type variables (cont.) • however, Comparable is itself a generic interface • moreover, any supertype of T may have extended it public static <T extends Object & // bounding class Comparable <? super T>> T min (T [ ] a) { . . . // the more general form T smallest = a [0]; for (int i = 1; i < a.length; i++) if (smallest.compareTo (a [i]) > 0) // T constraint smallest = a [i]; return smallest; } • cannot inherit multiple different instantiations of the same generic type (class or interface) • an inherited generic type is fixed for subtypes, too
  • 13. Page 12Classification: Restricted 12 Generic code and the JVM • the JVM has no instantiations of generic types • a generic type definition is compiled once only, and a corresponding raw type is produced • the name of the raw type is the same name but type variables removed • type variables are erased and replaced by their bounding types (or Object if no bounds); e.g.: class Pair { // the raw type for Pair <T> public Object first; public Object second; public Pair (Object f, Object s) { . . } } • byte code has some generic info, but objects don't
  • 14. Page 13Classification: Restricted 13 Generic code and the JVM (cont.) • Pair <String> and Pair <Employee> use the same bytecode generated as the raw class Pair • when translating generic expressions, such as Pair <Employee> buddies = new Pair < . .; Employee buddy = buddies.first; • the compiler uses the raw class and automatically inserts a cast from Object to Employee: Employee buddy = (Employee)buddies.first; • in C++, no such casts are required since class instantiations already use specific types • if multiple constraints (Object & Comparable. .) then the type parameter is replaced by the first one
  • 15. Page 14Classification: Restricted 14 Overriding of methods of generic type • consider a generic class with a non-final method: class Pair <T> { // parameter T is erased from code public void setSecond (T s) { second = s; } . . • to override such type-erased methods, the compiler must generate extra bridge methods: class DateInterval extends Pair <Date> { public void setSecond (Date high) { // override if (high.compareTo (first) < 0) throw new . . second = high; // otherwise OK } public void setSecond (Object s) { // bridge method setSecond ((Date)s); // generated by compiler } . .
  • 16. Page 15Classification: Restricted 15 Restrictions and limitations • in Java, generic types are compile-time entities • in C++, instantiations of a class template are compiled separately as source code, and tailored code is produced for each one • primitive type parameters (Pair <int>) not allowed • in C++, both classes and primitive types allowed • objects in JVM have non-generic classes: Pair<String> strPair = new Pair<String> . .; Pair<Number> numPair = new Pair<Number> . .; b = strPair.getClass () == numPair.getClass (); assert b == true; // both of the raw class Pair • but byte-code has reflective info about generics
  • 17. Page 16Classification: Restricted 16 Restrictions and limitations (cont.) • instantiations of generic parameter T are not allowed new T () // ERROR: whatever T to produce? new T [10] • arrays of parameterized types are not allowed new Pair <String> [10]; // ERROR • since type erasure removes type information needed for checks of array assignments • static fields and static methods with type parameters are not allowed class Singleton <T> { private static T singleOne; // ERROR • since after type erasure, one class and one shared static field for all instantiations and their objects
  • 18. Page 17Classification: Restricted 17 Wildcard types • note that the raw class Pair is not equal Pair <?> Pair pair1 = . .; pair1.first = new Double (10.0); // WARNING Pair <?> pair2 = . .; pair2.first = new Double (10.0); // ERROR • but some operations have no type constraints: public static boolean hasNulls (Pair <?> p) { return p.first == null || p.second == null; } • alternatively, you could provide a generic method public static <T> boolean hasNulls (Pair <T> p) • generally, prefer wildcard types (but use generic method with type T when multiple parameters)
  • 19. Page 18Classification: Restricted 18 Wildcard capture • the wildcard type ? cannot be used as a declared type of any variables (as in the previous slide) Pair <?> p = new Pair <String> ("one", "two"); . . p.first = p.second; // ERROR: unknown type • but, can sometimes use a generic method to capture the wildcard: public static <T> void rotate (Pair <T> p) { T temp = p.first; p.first = p.second; p.second = temp; } • the compile checks that such a capture is legal • e.g., the context ensures that T is unambiguous
  • 20. Page 19Classification: Restricted 19 Collections and algorithms • goal: design a minimal interface that you need • e.g., for max, implement to take any Collection public static <T extends Object & Comparable <? super T>> T max (Collection <? extends T> c) { // a hypothetical implementation: Iterator <T> it = c.iterator (); T largest = it.next (); // or throws NoSuchElement while (it.hasNext ()) { T val = it.next (); if (largest.compareTo (val) < 0) largest = val; } return largest; }