SlideShare a Scribd company logo
1 of 20
EFFECTIVE
JAVA
Roshan Deniyage
Axiohelix Co. Ltd
02/09/2010
Generics
Don’t use raw types in new codes
 Generics are a facility of generic programming that was added to the Java 
programming language in 2004 as part of J2SE 5.0.
 They allow "a type or method to operate on objects of various types while 
providing compile-time type safety.
What is Generics ?
public static void main(String args[]) {
List dataList = new ArrayList();
dataList.add("1");
dataList.add("test");
dataList.add(new Integer(10));
Integer value = (Integer) dataList.get(0);
}
Why need generics?
 This is old type of code which used before java SE5.
 Can you figure out the problem of this code??
// Throws ClassCastException
 So, programmers’ mistake can’t discover as soon as possible after they are 
made.  Exception can be thrown at run time.
 Therefore, a concept called generics was introduced to the java language in 
java SE5.
A class or interface whose declaration has one or more type parameters is a 
generic class or generic interface. (generic type)
Ex :
Generic interface declaration
public interface List<E> extends Collection<E> {
// Bulk of code is emitted
}
Generic class declaration
public class ArrayList<E> extends AbstractList<E> implements List<E>, Random-access,
Cloneable, java.io.Serializable {
// Bulk of code is emitted
}
Generic Types
Client Code
1.List<String> dataListWithGenerics = new ArrayList<String>();
// parameterized List
2.   List dataListWithoutGenerics = new ArrayList(); // raw type List
Both declarations are legal.
So, what is the difference ?
Number 1 is parameterized type representing List whose element type is 
“String”.
Number 2 is raw type representing List whose element type can be any type. 
(behave same as List representation before generic is added to the language)
Generic Types
public static <E> Set<E> union(Set<E> s1, Set<E> s2) {
Set<E> result = new HashSet<E>(s1);
result.addAll(s2);
return result;
}
This is discussed in Item 27
Generic Methods
Compiler use process called erasure.
(It erase all the generic type information at compile time)
 All the type information betweeen angle brackets is thrown out.
 All remaining uses of type variables are replaced by the upper bound of the type variable
(usually Object)
 Whenever the resulting code isn’t type-correct, a cast to the appropriate type is inserted
How generics work?
How generics work?
public String loophole(Integer x) {
List<String> ys = new LinkedList<String>();
List xs = ys;
xs.add(x); // compile-time unchecked warning
return ys.iterator().next();
}
public String loophole(Integer x) {
List ys = new LinkedList;
List xs = ys;
xs.add(x);
return (String) ys.iterator().next(); // run time error
}
Compiler change the source as follows
Let’s take an example :
If client code with raw type is as follows,
private final Collection stamps = new ArrayList() ;
stamps.add(new Coin());
Later if stamps is accessed as follows,
for (Iterator i = stamps.iterator(); i.hasNext(); ) {
Stamp s = (Stamp) i.next();
... // Do something with the stamp
}
// Accidently added code
What is the difference between the raw type
and parameterized type
// Throws ClassCastException
private final Collection<Stamp> stamps = new ArrayList<Stamp>() ;
stamps.add(new Coin());
for (Iterator i = stamps.iterator(); i.hasNext(); ) {
Stamp s = i.next();
... // Do something with the stamp
}
// Compile error will arise when you attempt to insert
Coin object to the collection
What happened if you replace raw type with
generics?
// No need to explicit casting, compiler will do that for you
Other benefits
// for-each loop over a parameterized collection - type safe
for (Stamp s : stamps) { // No cast
... // Do something with the stamp
}
// for loop with parameterized iterator declaration - type safe
for (Iterator<Stamp> i = stamps.iterator(); i.hasNext(); ) {
Stamp s = i.next(); // No cast necessary
... // Do something with the stamp
}
 If you use raw types, you lose all the safety and expressiveness
benefits of generics.
 Language provide raw types to support migration compatibility since
there are enormous amount of old codes exists.
 It had to be legal to pass instances of parameterized types to methods
that were designed for use with ordinary types, and vice versa.
 So instead of using raw types better to use parameterized types like,
 List<String>, List<Object>
 But there is a sub typing rule to consider.
 Although String is a subtype of Object List<String> is not a
subtype of List<Object>
 List<String> is a subtype of List
 List<String> is a subtype of List<?>
Note
In Item 25 these rules are discussed deeply.
Conclusion
 You lose type safety if you use a raw type like List, but not if you use
a parameterized type like List<Object>
Ex :
// Uses raw type (List) - fails at runtime
public static void main(String[] args) {
List<String> strings = new ArrayList<String>();
unsafeAdd(strings, new Integer(42));
String s = strings.get(0); // Compiler-generated cast
}
private static void unsafeAdd(List list, Object o) {
list.add(o); // you get a warning here since this is raw type
}
Best way to use
private static void unsafeAdd(List<Object> list, Object o) {
list.add(o);
}
 So, you will get a compile time error when try to add new Integer(42) object
to the list. So error can be detected earlier, not at run time.
 Anyway the above code doesn’t work, so working version of this code need
to be as follows,
Best way to use
Suggestion
public static void main(String[] args) {
List<String> strings = new ArrayList<String>();
safeAdd(strings, new Integer(42)); // compile time error detection
String s = strings.get(0);
}
private static <E> void safeAdd(List<E> list, E e) {
list.add(e);
}
Best way to use
// Unbounded wildcard type - type safe and flexible
int numElementsInCommon(Set<?> s1, Set<?> s2) {
int result = 0;
for (Object o1 : s1) {
if (s2.contains(o1))
result++;
}
return result;
}
When read data from a source
 Actual type of the ? will be detected by the compiler using a technique called
wild card detection.
 This will be explained in Item-27 clearly.
Benefit of using wildcard
 You can’t put any element (other than null) into a Collection<?>
(If you use wildcard declaration, preventing you from corrupting the
collection’s type invariant)
Limitation of the wildcard
 If need can’t put any element other than null.
 Can’t assume anything about the type of the objects that you get out.
If these restrictions are unacceptable, you can use generic methods (Item 27)
or bounded wildcard types (Item 28)
Exceptions (When you must use raw types)
1. You must use raw types in class literals
The specification does not permit the use of parameterized types in class
literals.
 List.class, String[].class, and int.class - are all legal
 List<String>.class and List<?>.class - are not legal
2. This is the preferred way to use the instanceof operator with generic
types
// Legitimate use of raw type - instanceof operator
if (o instanceof Set) { // Raw type
Set<?> m = (Set<?>) o; // Wildcard type
}
Summary
 Using raw types can lead to exceptions at runtime, so don’t use them in new
code.
 They are provided only for compatibility and interoperability with legacy code
that predates the introduction of generics.
Safe Type Declarations
Set<Object> - Parameterized type representing a set that can contain
objects of any type.
Set<?> - Wildcard type representing a set that can contain only objects
of some unknown type.
Unsafe Type Declaration
Set - Raw type
Term Example Item
Parameterized type List<String> Item 23
Actual type parameter String Item 23
Generic type List<E> Items 23, 26
Formal type parameter E Item 23
Unbounded wildcard type List<?> Item 23
Raw type List Item 23
Bounded type parameter <E extends Number> Item 26
Recursive type bound <T extends Comparable<T>> Item 27
Bounded wildcard type List<? extends Number> Item 28
Generic method static <E> List<E> asList(E[] a) Item 27
Type token String.class Item 29
Summary (Cont…) / New terms introduced
THANK YOU!

More Related Content

What's hot

Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets Hitesh-Java
 
Java 8 - Features Overview
Java 8 - Features OverviewJava 8 - Features Overview
Java 8 - Features OverviewSergii Stets
 
java 8 new features
java 8 new features java 8 new features
java 8 new features Rohit Verma
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesGanesh Samarthyam
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default argumentsNikhil Pandit
 
Polimorfismo em java
Polimorfismo em javaPolimorfismo em java
Polimorfismo em javaManoel Afonso
 
Effective Java - Chapter 3: Methods Common to All Objects
Effective Java - Chapter 3: Methods Common to All ObjectsEffective Java - Chapter 3: Methods Common to All Objects
Effective Java - Chapter 3: Methods Common to All Objectsİbrahim Kürce
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template LibraryKumar Gaurav
 
Collection v3
Collection v3Collection v3
Collection v3Sunil OS
 

What's hot (20)

Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 
Core java
Core java Core java
Core java
 
Java interface
Java interfaceJava interface
Java interface
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
Java 8 - Features Overview
Java 8 - Features OverviewJava 8 - Features Overview
Java 8 - Features Overview
 
java 8 new features
java 8 new features java 8 new features
java 8 new features
 
Psi m14
Psi m14Psi m14
Psi m14
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Polimorfismo em java
Polimorfismo em javaPolimorfismo em java
Polimorfismo em java
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
Effective Java - Chapter 3: Methods Common to All Objects
Effective Java - Chapter 3: Methods Common to All ObjectsEffective Java - Chapter 3: Methods Common to All Objects
Effective Java - Chapter 3: Methods Common to All Objects
 
Pilha e filas
Pilha e filasPilha e filas
Pilha e filas
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
 
Java generics
Java genericsJava generics
Java generics
 
07 java collection
07 java collection07 java collection
07 java collection
 
Collection v3
Collection v3Collection v3
Collection v3
 

Viewers also liked

Effective java 1 and 2
Effective java 1 and 2Effective java 1 and 2
Effective java 1 and 2중선 곽
 
The Go Programing Language 1
The Go Programing Language 1The Go Programing Language 1
The Go Programing Language 1İbrahim Kürce
 
Effective Java - Enum and Annotations
Effective Java - Enum and AnnotationsEffective Java - Enum and Annotations
Effective Java - Enum and AnnotationsRoshan Deniyage
 
Effective Java - Chapter 2: Creating and Destroying Objects
Effective Java - Chapter 2: Creating and Destroying ObjectsEffective Java - Chapter 2: Creating and Destroying Objects
Effective Java - Chapter 2: Creating and Destroying Objectsİbrahim Kürce
 
Effective Java - Chapter 4: Classes and Interfaces
Effective Java - Chapter 4: Classes and InterfacesEffective Java - Chapter 4: Classes and Interfaces
Effective Java - Chapter 4: Classes and Interfacesİbrahim Kürce
 
Effective java
Effective javaEffective java
Effective javaEmprovise
 
Effective java
Effective javaEffective java
Effective javaHaeil Yi
 

Viewers also liked (8)

Effective java 1 and 2
Effective java 1 and 2Effective java 1 and 2
Effective java 1 and 2
 
The Go Programing Language 1
The Go Programing Language 1The Go Programing Language 1
The Go Programing Language 1
 
Effective Java - Enum and Annotations
Effective Java - Enum and AnnotationsEffective Java - Enum and Annotations
Effective Java - Enum and Annotations
 
Effective Java - Chapter 2: Creating and Destroying Objects
Effective Java - Chapter 2: Creating and Destroying ObjectsEffective Java - Chapter 2: Creating and Destroying Objects
Effective Java - Chapter 2: Creating and Destroying Objects
 
Effective Java - Chapter 4: Classes and Interfaces
Effective Java - Chapter 4: Classes and InterfacesEffective Java - Chapter 4: Classes and Interfaces
Effective Java - Chapter 4: Classes and Interfaces
 
Effective java
Effective javaEffective java
Effective java
 
Effective Java
Effective JavaEffective Java
Effective Java
 
Effective java
Effective javaEffective java
Effective java
 

Similar to Effective Java - Generics

Effective Java Second Edition
Effective Java Second EditionEffective Java Second Edition
Effective Java Second Editionlosalamos
 
Java New Programming Features
Java New Programming FeaturesJava New Programming Features
Java New Programming Featurestarun308
 
Javase5generics
Javase5genericsJavase5generics
Javase5genericsimypraz
 
JAVA Tutorial- Do's and Don'ts of Java programming
JAVA Tutorial- Do's and Don'ts of Java programmingJAVA Tutorial- Do's and Don'ts of Java programming
JAVA Tutorial- Do's and Don'ts of Java programmingKeshav Kumar
 
JAVA Tutorial- Do's and Don'ts of Java programming
JAVA Tutorial- Do's and Don'ts of Java programmingJAVA Tutorial- Do's and Don'ts of Java programming
JAVA Tutorial- Do's and Don'ts of Java programmingKeshav Kumar
 
Jdk1.5 Features
Jdk1.5 FeaturesJdk1.5 Features
Jdk1.5 Featuresindia_mani
 
javasebeyondbasics
javasebeyondbasicsjavasebeyondbasics
javasebeyondbasicswebuploader
 
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
 
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 Generics.ppt
Java Generics.pptJava Generics.ppt
Java Generics.pptbrayazar
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first stepsRenato Primavera
 
Introduction to Intermediate Java
Introduction to Intermediate JavaIntroduction to Intermediate Java
Introduction to Intermediate JavaPhilip Johnson
 

Similar to Effective Java - Generics (20)

Effective Java Second Edition
Effective Java Second EditionEffective Java Second Edition
Effective Java Second Edition
 
Java New Programming Features
Java New Programming FeaturesJava New Programming Features
Java New Programming Features
 
Javase5generics
Javase5genericsJavase5generics
Javase5generics
 
JAVA Tutorial- Do's and Don'ts of Java programming
JAVA Tutorial- Do's and Don'ts of Java programmingJAVA Tutorial- Do's and Don'ts of Java programming
JAVA Tutorial- Do's and Don'ts of Java programming
 
JAVA Tutorial- Do's and Don'ts of Java programming
JAVA Tutorial- Do's and Don'ts of Java programmingJAVA Tutorial- Do's and Don'ts of Java programming
JAVA Tutorial- Do's and Don'ts of Java programming
 
Jdk1.5 Features
Jdk1.5 FeaturesJdk1.5 Features
Jdk1.5 Features
 
javasebeyondbasics
javasebeyondbasicsjavasebeyondbasics
javasebeyondbasics
 
Java generics final
Java generics finalJava generics final
Java generics final
 
Java 5 Features
Java 5 FeaturesJava 5 Features
Java 5 Features
 
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)
 
Java Generics
Java GenericsJava Generics
Java Generics
 
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)
 
Java Generics.ppt
Java Generics.pptJava Generics.ppt
Java Generics.ppt
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
 
Java API, Exceptions and IO
Java API, Exceptions and IOJava API, Exceptions and IO
Java API, Exceptions and IO
 
Introduction to Intermediate Java
Introduction to Intermediate JavaIntroduction to Intermediate Java
Introduction to Intermediate Java
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Generics
GenericsGenerics
Generics
 

Recently uploaded

Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 

Recently uploaded (20)

Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 

Effective Java - Generics

  • 1. EFFECTIVE JAVA Roshan Deniyage Axiohelix Co. Ltd 02/09/2010 Generics Don’t use raw types in new codes
  • 2.  Generics are a facility of generic programming that was added to the Java  programming language in 2004 as part of J2SE 5.0.  They allow "a type or method to operate on objects of various types while  providing compile-time type safety. What is Generics ?
  • 3. public static void main(String args[]) { List dataList = new ArrayList(); dataList.add("1"); dataList.add("test"); dataList.add(new Integer(10)); Integer value = (Integer) dataList.get(0); } Why need generics?  This is old type of code which used before java SE5.  Can you figure out the problem of this code?? // Throws ClassCastException  So, programmers’ mistake can’t discover as soon as possible after they are  made.  Exception can be thrown at run time.  Therefore, a concept called generics was introduced to the java language in  java SE5.
  • 4. A class or interface whose declaration has one or more type parameters is a  generic class or generic interface. (generic type) Ex : Generic interface declaration public interface List<E> extends Collection<E> { // Bulk of code is emitted } Generic class declaration public class ArrayList<E> extends AbstractList<E> implements List<E>, Random-access, Cloneable, java.io.Serializable { // Bulk of code is emitted } Generic Types
  • 5. Client Code 1.List<String> dataListWithGenerics = new ArrayList<String>(); // parameterized List 2.   List dataListWithoutGenerics = new ArrayList(); // raw type List Both declarations are legal. So, what is the difference ? Number 1 is parameterized type representing List whose element type is  “String”. Number 2 is raw type representing List whose element type can be any type.  (behave same as List representation before generic is added to the language) Generic Types
  • 6. public static <E> Set<E> union(Set<E> s1, Set<E> s2) { Set<E> result = new HashSet<E>(s1); result.addAll(s2); return result; } This is discussed in Item 27 Generic Methods
  • 7. Compiler use process called erasure. (It erase all the generic type information at compile time)  All the type information betweeen angle brackets is thrown out.  All remaining uses of type variables are replaced by the upper bound of the type variable (usually Object)  Whenever the resulting code isn’t type-correct, a cast to the appropriate type is inserted How generics work?
  • 8. How generics work? public String loophole(Integer x) { List<String> ys = new LinkedList<String>(); List xs = ys; xs.add(x); // compile-time unchecked warning return ys.iterator().next(); } public String loophole(Integer x) { List ys = new LinkedList; List xs = ys; xs.add(x); return (String) ys.iterator().next(); // run time error } Compiler change the source as follows
  • 9. Let’s take an example : If client code with raw type is as follows, private final Collection stamps = new ArrayList() ; stamps.add(new Coin()); Later if stamps is accessed as follows, for (Iterator i = stamps.iterator(); i.hasNext(); ) { Stamp s = (Stamp) i.next(); ... // Do something with the stamp } // Accidently added code What is the difference between the raw type and parameterized type // Throws ClassCastException
  • 10. private final Collection<Stamp> stamps = new ArrayList<Stamp>() ; stamps.add(new Coin()); for (Iterator i = stamps.iterator(); i.hasNext(); ) { Stamp s = i.next(); ... // Do something with the stamp } // Compile error will arise when you attempt to insert Coin object to the collection What happened if you replace raw type with generics? // No need to explicit casting, compiler will do that for you Other benefits // for-each loop over a parameterized collection - type safe for (Stamp s : stamps) { // No cast ... // Do something with the stamp } // for loop with parameterized iterator declaration - type safe for (Iterator<Stamp> i = stamps.iterator(); i.hasNext(); ) { Stamp s = i.next(); // No cast necessary ... // Do something with the stamp }
  • 11.  If you use raw types, you lose all the safety and expressiveness benefits of generics.  Language provide raw types to support migration compatibility since there are enormous amount of old codes exists.  It had to be legal to pass instances of parameterized types to methods that were designed for use with ordinary types, and vice versa.  So instead of using raw types better to use parameterized types like,  List<String>, List<Object>  But there is a sub typing rule to consider.  Although String is a subtype of Object List<String> is not a subtype of List<Object>  List<String> is a subtype of List  List<String> is a subtype of List<?> Note In Item 25 these rules are discussed deeply. Conclusion
  • 12.  You lose type safety if you use a raw type like List, but not if you use a parameterized type like List<Object> Ex : // Uses raw type (List) - fails at runtime public static void main(String[] args) { List<String> strings = new ArrayList<String>(); unsafeAdd(strings, new Integer(42)); String s = strings.get(0); // Compiler-generated cast } private static void unsafeAdd(List list, Object o) { list.add(o); // you get a warning here since this is raw type } Best way to use
  • 13. private static void unsafeAdd(List<Object> list, Object o) { list.add(o); }  So, you will get a compile time error when try to add new Integer(42) object to the list. So error can be detected earlier, not at run time.  Anyway the above code doesn’t work, so working version of this code need to be as follows, Best way to use
  • 14. Suggestion public static void main(String[] args) { List<String> strings = new ArrayList<String>(); safeAdd(strings, new Integer(42)); // compile time error detection String s = strings.get(0); } private static <E> void safeAdd(List<E> list, E e) { list.add(e); } Best way to use
  • 15. // Unbounded wildcard type - type safe and flexible int numElementsInCommon(Set<?> s1, Set<?> s2) { int result = 0; for (Object o1 : s1) { if (s2.contains(o1)) result++; } return result; } When read data from a source  Actual type of the ? will be detected by the compiler using a technique called wild card detection.  This will be explained in Item-27 clearly.
  • 16. Benefit of using wildcard  You can’t put any element (other than null) into a Collection<?> (If you use wildcard declaration, preventing you from corrupting the collection’s type invariant) Limitation of the wildcard  If need can’t put any element other than null.  Can’t assume anything about the type of the objects that you get out. If these restrictions are unacceptable, you can use generic methods (Item 27) or bounded wildcard types (Item 28)
  • 17. Exceptions (When you must use raw types) 1. You must use raw types in class literals The specification does not permit the use of parameterized types in class literals.  List.class, String[].class, and int.class - are all legal  List<String>.class and List<?>.class - are not legal 2. This is the preferred way to use the instanceof operator with generic types // Legitimate use of raw type - instanceof operator if (o instanceof Set) { // Raw type Set<?> m = (Set<?>) o; // Wildcard type }
  • 18. Summary  Using raw types can lead to exceptions at runtime, so don’t use them in new code.  They are provided only for compatibility and interoperability with legacy code that predates the introduction of generics. Safe Type Declarations Set<Object> - Parameterized type representing a set that can contain objects of any type. Set<?> - Wildcard type representing a set that can contain only objects of some unknown type. Unsafe Type Declaration Set - Raw type
  • 19. Term Example Item Parameterized type List<String> Item 23 Actual type parameter String Item 23 Generic type List<E> Items 23, 26 Formal type parameter E Item 23 Unbounded wildcard type List<?> Item 23 Raw type List Item 23 Bounded type parameter <E extends Number> Item 26 Recursive type bound <T extends Comparable<T>> Item 27 Bounded wildcard type List<? extends Number> Item 28 Generic method static <E> List<E> asList(E[] a) Item 27 Type token String.class Item 29 Summary (Cont…) / New terms introduced