Java Generics. Lecture CS1122 Summer 2008

Similar documents
Generics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Generics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Java Generics Java, summer semester

JAVA V Source files Java, winter semester

JAVA V Assertions Java, winter semester

The collections interfaces

Subtyping for behaviour?

Parametric polymorphism and Generics

11/7/18 JAVA GENERICS. Java Collections. Java Collections. Using Java Collections. Proposals for adding Generics to Java.

Lecture Outline. Parametric Polymorphism and Java Generics. Polymorphism. Polymorphism

Photo credit: Andrew Kennedy JAVA GENERICS

CSE 331 Software Design & Implementation

#Students per correct answers

PARAMETRIC POLYMORPHISM

Java. Platforms, etc.

Java. Platforms, etc.

Announcements. Lecture 15 Generics 2. Announcements. Big picture. CSE 331 Software Design and Implementation

CSE 331 Software Design and Implementation. Lecture 15 Generics 2

Subtyping for behaviour?

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

Outline. 1 Generic Classes. 2 Generics and Subtyping. Generics. Guy Wiener. Generic Classes. Generics and Subtyping

Polymorphism (generics)

Subtyping for behaviour?

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 19 19:36: CS61B: Lecture #25 1

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 18 21:04: CS61B: Lecture #25 1

CSE 331. Generics (Parametric Polymorphism)

Introduction to Generics. Defining a Class with a Type Parameter

Java: exceptions and genericity

Exam 2. Topics. Preconditions vs. Exceptions. Exam 2 Review. Exam 2 on Thursday, March 29 th. Closed book, closed computer, closed phone

CSE 331 Software Design & Implementation

Software Engineering. Prof. Agostino Poggi

CSCE 314 Programming Languages

Java 5 New Language Features

PIC 20A Collections and Data Structures

Exam Review. CSE 331 Section 10 12/6/12. Slides by Kellen Donohue with material from Mike Ernst

boolean add(e e); // Makes the collection contain e. // Returns true if e not already there.

CSE 331 Software Design & Implementation

CSC 172 Data Structures and Algorithms. Lecture 3 Spring 2018 TuTh 3:25 pm 4:40 pm

CS61B Lecture #24. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

Announcements. Java Graphics. Exceptions. Java Odds & Ends

CSE Lecture 7: Polymorphism and generics 16 September Nate Nystrom UTA

CS61B Lecture #23. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

CSE 331 Software Design and Implementation. Lecture 13 Generics 1

Announcements. Lecture 14 Generics 1. Announcements. CSE 331 Software Design and Implementation. Leah Perlmutter / Summer 2018

CSE 331 Software Design and Implementation. Lecture 14 Generics 1

CS6202: Advanced Topics in Programming Languages and Systems Lecture 0 : Overview

Computational Expression

Chapter 3 Function Overloading

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

Lecture #23: Conversion and Type Inference

CMSC 132: Object-Oriented Programming II. Generic Programming. Department of Computer Science University of Maryland, College Park

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

INF3110 Programming Languages Object Orientation and Types, part II

Inheritance. Improving Structure with Inheritance. Dr. Siobhán Drohan Mairead Meagher. Produced by:

DrStrangebrac<E,T> Or how I learned to stop worrying, and love generics...

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

1.1. Annotations History Lesson - C/C++

4. Java language basics: Function. Minhaeng Lee

CS108, Stanford Handout #8. Java Generics

MIT Semantic Analysis. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Exercise 8 Parametric polymorphism November 17, 2017

Genericity. Philippe Collet. Master 1 IFI Interna3onal h9p://dep3nfo.unice.fr/twiki/bin/view/minfo/sofeng1314. P.

Java Generics -- an introduction. Based on

Module 11. Collections and Iterators. Adapted from Absolute Java, Rose Williams, Binghamton University

Problems. Java Generics. Example. Example. Often you need the same behavior for different kind of classes

Generic types. Announcements. Raw ArrayLists. Generic types (cont.) Creating a raw ArrayList: Accessing a raw ArrayList:

Closed book but one sheet, both sides, of A4 paper is allowed. Section 2.5 of the text Generics in the Java Programming Languages by Gilad Bracha

COMP6700/2140 Generic Methods

CMSC 202. Containers

Collections, Maps and Generics

ABSTRACT DATA TYPES: COLLECTIONS, LISTS, SETS, MAP, QUEUES. Thursday, June 30, 2011

INF3110 Programming Languages Object orientation part II

Exercise 8 Parametric polymorphism November 18, 2016

Pierce Ch. 3, 8, 11, 15. Type Systems

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Java Collection Framework

CS558 Programming Languages

Big O & ArrayList Fall 2018 Margaret Reid-Miller

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

CS Programming I: ArrayList

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

301AA - Advanced Programming [AP-2017]

Abstract Data Types (ADTs) Example ADTs. Using an Abstract Data Type. Class #08: Linear Data Structures

CSE 8B Intro to CS: Java

ITI Introduction to Computing II

publicvoid set(objectobject){ this. object =object;} public Objectget(){ return object;}

09/02/2013 TYPE CHECKING AND CASTING. Lecture 5 CS2110 Spring 2013

Java Brand Generics. Advanced Topics in Java. Khalid Azim Mughal Version date:

1 10 3:30 5: :30 1:30 206, ICICS/CS

More than you ever wanted to know about. Java Generics. Jeff Meister CMSC 420 Summer 2007

CS 302: Introduction to Programming

301AA - Advanced Programming [AP-2017]

Algorithms. Produced by. Eamonn de Leastar

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

CompuScholar, Inc. Alignment to Nevada "Computer Science" Course Standards

The class Object. Lecture CS1122 Summer 2008

Types. What is a type?

Science of Computer Programming

Transcription:

Java Generics http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf Lecture 17 -- CS1122 Summer 2008

Java Generics quick review When you see <... > in Java, you are dealing with generics Generics allow you to abstract over types Do not think about hierarchy in terms of the formal type parameters (stuff in < >) Each generic type is unique

Last time we talked about... Defining simple generics. Formal type parameters come between?

Last time we talked about... Defining simple generics. Formal type parameters come between? <... >

Last time we talked about... Defining simple generics. Formal type parameters come between? <... > Formal type parameters are usually named with?

Last time we talked about... Defining simple generics. Formal type parameters come between? <... > Formal type parameters are usually named with? single capital letters

Last time we talked about... Defining simple generics. Formal type parameters come between? <... > Formal type parameters are usually named with? single capital letters Generics and subtyping. Is List<SomeClass> a subtype of List<Object>?

Last time we talked about... Defining simple generics. Formal type parameters come between? <... > Formal type parameters are usually named with? single capital letters Generics and subtyping. Is List<SomeClass> a subtype of List<Object>? no

Last time we talked about... Defining simple generics. Formal type parameters come between? <... > Formal type parameters are usually named with? single capital letters Generics and subtyping. Is List<SomeClass> a subtype of List<Object>? no Wildcards What character is used in wild cards?

Last time we talked about... Defining simple generics. Formal type parameters come between? <... > Formal type parameters are usually named with? single capital letters Generics and subtyping. Is List<SomeClass> a subtype of List<Object>? no Wildcards What character is used in wild cards? the question mark. ex: List<?>

Last time we talked about... Defining simple generics. Formal type parameters come between? <... > Formal type parameters are usually named with? single capital letters Generics and subtyping. Is List<SomeClass> a subtype of List<Object>? no Wildcards What character is used in wild cards? the question mark. ex: List<?> What is different about a bounded wildcard?

Last time we talked about... Defining simple generics. Formal type parameters come between? <... > Formal type parameters are usually named with? single capital letters Generics and subtyping. Is List<SomeClass> a subtype of List<Object>? no Wildcards What character is used in wild cards? the question mark. ex: List<?> What is different about a bounded wildcard? List<? extends Shape>

Generic Methods static void fromarraytocollection(object[] a, Collection<?> c){ for (Object o : a) { c.add(o); // compile time error

Generic Methods static void fromarraytocollection(object[] a, Collection<?> c){ for (Object o : a) { c.add(o); // compile time error Can t add an Object to a Collection of?

Generic Methods static <T> void fromarraytocollection(t[] a, Collection<T> c){ for (T o : a) { c.add(o); Now we have a type named T We can add objects of type T to a Collection declared with type T

Sample Code Object[] oa = new Object[100]; Collection<Object> co = new ArrayList<Object>(); //T inferred to be fromarraytocollection(oa, co);

Sample Code Object[] oa = new Object[100]; Collection<Object> co = new ArrayList<Object>(); //T inferred to be Object fromarraytocollection(oa, co);

Sample Code Object[] oa = new Object[100]; Collection<Object> co = new ArrayList<Object>(); //T inferred to be Object fromarraytocollection(oa, co); String[] sa = new String[100]; Collection<String> cs = new ArrayList<String>(); // T inferred to be fromarraytocollection(sa, cs); // T inferred to be fromarraytocollection(sa, co);

Sample Code Object[] oa = new Object[100]; Collection<Object> co = new ArrayList<Object>(); //T inferred to be Object fromarraytocollection(oa, co); String[] sa = new String[100]; Collection<String> cs = new ArrayList<String>(); // T inferred to be String fromarraytocollection(sa, cs); // T inferred to be Object fromarraytocollection(sa, co);

Sample Code Object[] oa = new Object[100]; Collection<Object> co = new ArrayList<Object>(); Integer[] ia = new Integer[100]; Float[] fa = new Float[100]; Number[] na = new Number[100]; Collection<Number> cn = new ArrayList<Number>(); //T inferred to be fromarraytocollection(ia, cn); // T inferred to be fromarraytocollection(fa, cn);

Sample Code Object[] oa = new Object[100]; Collection<Object> co = new ArrayList<Object>(); Integer[] ia = new Integer[100]; Float[] fa = new Float[100]; Number[] na = new Number[100]; Collection<Number> cn = new ArrayList<Number>(); //T inferred to be Number fromarraytocollection(ia, cn); // T inferred to be Number fromarraytocollection(fa, cn);

Sample Code Object[] oa = new Object[100]; Collection<Object> co = new ArrayList<Object>(); Integer[] ia = new Integer[100]; Float[] fa = new Float[100]; Number[] na = new Number[100]; Collection<Number> cn = new ArrayList<Number>(); // T inferred to be fromarraytocollection(na, cn); // T inferred to be fromarraytocollection(na, co);

Sample Code Object[] oa = new Object[100]; Collection<Object> co = new ArrayList<Object>(); Integer[] ia = new Integer[100]; Float[] fa = new Float[100]; Number[] na = new Number[100]; Collection<Number> cn = new ArrayList<Number>(); // T inferred to be Number fromarraytocollection(na, cn); // T inferred to be Object fromarraytocollection(na, co);

Sample Code Collection<String> cs = new ArrayList<String>(); Number[] na = new Number[100]; // does this work? -- NO fromarraytocollection(na, cs); There is no relation between String and Number, so no type can be resolved for T and a compiler error results.

Generic Methods vs. Wildcards Consider the following... interface Collection<E> { public boolean containsall(collection<?> c); public boolean addall(collection<? extends E> c); Compare to: interface Collection<E> { public <T> boolean containsall(collection<t> c); //type variables can have bounds too! public <T extends E> boolean addall(collection<t> c);

Generic Methods vs. Wildcards interface Collection<E> { public <T> boolean containsall(collection<t> c); //type variables can have bounds too! public <T extends E> boolean addall(collection<t> c); In both methods, T is used only once. Return type doesn t depend on T. Infer... T is being used for polymorphism. Allows us to pass a variety of types to the parameter. This is a better place for wildcards.

Generic Methods allow us to express dependencies among types of among parameters and/or return type of method if this dependency doesn t exist, use wildcards

Generic Methods AND Wildcards class Collections { public static <T> void copy(list<t> dest, List<? extends T> src){ //code omitted Why have we used a generic method?

Generic Methods AND Wildcards class Collections { public static <T> void copy(list<t> dest, List<? extends T> src){ //code omitted Why have we used a generic method? the parameters depend on each other

Generic Methods AND Wildcards class Collections { public static <T> void copy(list<t> dest, List<? extends T> src){ //code omitted Compare to... class Collections { public static <T, S extends T> void copy (List<T> dest, List<S> src){ //code omitted

Generic Methods AND Wildcards class Collections { public static <T, S extends T> void copy (List<T> dest, List<S> src){ //code omitted Type S is only used once. Only the type of src is related to S. Replace S with a wildcard. Makes code clearer and more concise.

Generic Methods AND Wildcards static List<List<? extends Shape>> history = new ArrayList<List<? extends Shape>>(); public void drawall(list<? extends Shape> shapes) { history.addlast(shapes); for (Shape s: shapes) { s.draw(this);

Summary Use generic methods when the types of multiple parameters are related. Use generic methods when the return type of the method is related to the types of one or more parameters. You can use generic methods and wildcards together. Use wildcards to make code clearer and more concise.