Java Generics. Lecture CS1122 Summer 2008
|
|
- Wendy Todd
- 6 years ago
- Views:
Transcription
1 Java Generics Lecture CS1122 Summer 2008
2 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
3 Last time we talked about... Defining simple generics. Formal type parameters come between?
4 Last time we talked about... Defining simple generics. Formal type parameters come between? <... >
5 Last time we talked about... Defining simple generics. Formal type parameters come between? <... > Formal type parameters are usually named with?
6 Last time we talked about... Defining simple generics. Formal type parameters come between? <... > Formal type parameters are usually named with? single capital letters
7 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>?
8 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
9 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?
10 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<?>
11 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?
12 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>
13 Generic Methods static void fromarraytocollection(object[] a, Collection<?> c){ for (Object o : a) { c.add(o); // compile time error
14 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?
15 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
16 Sample Code Object[] oa = new Object[100]; Collection<Object> co = new ArrayList<Object>(); //T inferred to be fromarraytocollection(oa, co);
17 Sample Code Object[] oa = new Object[100]; Collection<Object> co = new ArrayList<Object>(); //T inferred to be Object fromarraytocollection(oa, co);
18 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);
19 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);
20 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);
21 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);
22 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);
23 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);
24 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.
25 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);
26 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.
27 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
28 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?
29 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
30 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
31 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.
32 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);
33 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.
Generics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון
Generics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון 2 Example List myintlist = new LinkedList(); // 1 myintlist.add(new Integer(0)); // 2 Integer x = (Integer) myintlist.iterator().next(); // 3
More informationGenerics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון
Generics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון 2 Example List myintlist = new LinkedList(); // 1 myintlist.add(new Integer(0)); // 2 Integer x = myintlist.iterator().next(); // 3 3 Example
More informationJava Generics Java, summer semester
Java Generics Introduction similar to the templates in C#/C++ but only on first view typed arguments goal clear code type safety Motivational example without generics (
More informationJAVA V Source files Java, winter semester
JAVA Source files 17.10.2017 1 Unicode programs ~ Unicode comments, identifiers, char and string constants the rest is in ASCII (
More informationJAVA V Assertions Java, winter semester
JAVA Assertions 1 Assertion since Java 1.4 the statement with a boolean expression a developer supposes that the expression is always satisfied (evaluates to true) if it is evaluated to false -> error
More informationThe collections interfaces
Generics in Java Advanced Programming 4/18/16 1 The collections interfaces Contains unique elements Maps unique keys to values 4/18/16 2 Collections in Java Array has a special language support Iterators
More informationSubtyping for behaviour?
Subtyping for behaviour? 10/26/10 1 Subtyping for behaviour the inner style class Reservation { date... ; customer... ; void print() { // print Date and Customer inner; class FlightReservation extends
More informationParametric polymorphism and Generics
Parametric polymorphism and Generics Today s Lecture Outline Parametric polymorphism Java generics Declaring and instantiating generics Bounded types: restricting instantiations Generics and subtyping.
More information11/7/18 JAVA GENERICS. Java Collections. Java Collections. Using Java Collections. Proposals for adding Generics to Java.
JAVA GENERICS Lecture CS110 Fall 018 Photo credit: Andrew Kennedy Java Collections Early versions of Java lacked generics interface Collection { /** Return true iff the collection contains ob */ boolean
More informationLecture Outline. Parametric Polymorphism and Java Generics. Polymorphism. Polymorphism
Lecture Outline Parametric Polymorphism and Java Generics Parametric polymorphism Java generics Declaring and instantiating generics Bounded types: restricting instantiations Generics and subtyping. Wildcards
More informationPhoto credit: Andrew Kennedy JAVA GENERICS
Photo credit: Andrew Kennedy JAVA GENERICS Lecture 17 CS2110 Spring 2017 Java Collections 2 Early versions of Java lacked generics interface Collection { /** Return true iff the collection contains ob
More informationCSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation Hal Perkins Autumn 2013 Generics (Polymorphism) (Slides by Mike Ernst and David Notkin) 1 Varieties of abstraction Abstraction over computation: procedures int
More information#Students per correct answers
Generics in Java Programming Projects 14/04/15 1 #Students per correct answers Quiz"2" 10" 9" 8" 7" 6" 5" 4" 3" 2" 1" 0" 10" 9" 8" 7" 6" 5" 4" 3" 2" 1" 0" #"Correct"Answers" 10" 9" 8" 7" 6" 5" 4" 3" 2"
More informationPARAMETRIC POLYMORPHISM
PARAMETRIC POLYMORPHISM Java C#! Parametric polymorphism: " Java Generics and Generic C# for.net! The idea: the compiler is able to check parametric classes just looking at their definilon 1 Java Generics
More informationJava. Platforms, etc.
Java Platforms, etc. Java object oriented (almost) all is object interpreted source code (.java) compiled to the bytecode bytecode (.class) interpreted by the virtual machine platform independent programs
More informationJava. Platforms, etc.
Java Platforms, etc. Java object oriented (almost) all is object interpreted source code (.java) compiled to the bytecode bytecode (.class) interpreted by the virtual machine platform independent programs
More informationAnnouncements. Lecture 15 Generics 2. Announcements. Big picture. CSE 331 Software Design and Implementation
CSE 331 Software Design and Implementation Lecture 15 Generics 2 Announcements Leah Perlmutter / Summer 2018 Announcements Quiz 5 is due tomorrow Homework 6 due tomorrow Section tomorrow! Subtyping now
More informationCSE 331 Software Design and Implementation. Lecture 15 Generics 2
CSE 331 Software Design and Implementation Lecture 15 Generics 2 Leah Perlmutter / Summer 2018 Announcements Announcements Quiz 5 is due tomorrow Homework 6 due tomorrow Section tomorrow! Subtyping now
More informationSubtyping for behaviour?
Subtyping for behaviour? Reservation date customer print() FlightReservation flight seat print() TrainReservation train waggon seat print() 10/23/2009 1 Subtyping for behaviour the inner style class Reservation
More informationCSE 331 Software Design and Implementation. Lecture 14 Generics 2
CSE 331 Software Design and Implementation Lecture 14 Generics 2 Zach Tatlock / Spring 2018 Big picture Last time: Generics intro Subtyping and Generics Using bounds for more flexible subtyping Using wildcards
More informationCSE 331 Software Design and Implementation. Lecture 14 Generics 2
CSE 331 Software Design and Implementation Lecture 14 Generics 2 James Wilcox / Winter 2016 Hi, I m James! Big picture Last time: Generics intro Subtyping and Generics Using bounds for more flexible subtyping
More informationOutline. 1 Generic Classes. 2 Generics and Subtyping. Generics. Guy Wiener. Generic Classes. Generics and Subtyping
Outline 1 2 Outline 1 2 (Parametric) classes are classes that have Parameters A generic class with a concrete type parameter is an instance of the generic class The type parameter can be the type of variables,
More informationPolymorphism (generics)
Polymorphism (generics) CSE 331 University of Washington Michael Ernst Varieties of abstraction Abstraction over computation: procedures (methods) int x1, y1, x2, y2; Math.sqrt(x1*x1 + y1*y1); Math.sqrt(x2*x2
More informationSubtyping for behaviour?
Subtyping for behaviour? Reservation date customer FlightReservation flight seat print() print() TrainReservation train waggon seat print() 10/4/2004 1 Subtyping for behaviour BETA style class Reservation
More informationCS61B Lecture #25: Java Generics. Last modified: Thu Oct 19 19:36: CS61B: Lecture #25 1
CS61B Lecture #25: Java Generics Last modified: Thu Oct 19 19:36:29 2017 CS61B: Lecture #25 1 The Old Days Java library types such as List didn t used to be parameterized. All Lists were lists of Objects.
More informationCS61B Lecture #25: Java Generics. Last modified: Thu Oct 18 21:04: CS61B: Lecture #25 1
CS61B Lecture #25: Java Generics Last modified: Thu Oct 18 21:04:53 2018 CS61B: Lecture #25 1 The Old Days Java library types such as List didn t used to be parameterized. All Lists were lists of Objects.
More informationCSE 331. Generics (Parametric Polymorphism)
CSE 331 Generics (Parametric Polymorphism) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia http://www.cs.washington.edu/331/ 1 Parametric polymorphism
More informationIntroduction to Generics. Defining a Class with a Type Parameter
Introduction to Generics Beginning with version 5.0, Java allows class and method definitions that include parameters for types Such definitions are called generics Generic programming with a type parameter
More informationJava: exceptions and genericity
Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: exceptions and genericity Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Exceptions Exceptions
More informationExam 2. Topics. Preconditions vs. Exceptions. Exam 2 Review. Exam 2 on Thursday, March 29 th. Closed book, closed computer, closed phone
Exam 2 Exam 2 on Thursday, March 29 th Exam 2 Review Closed book, closed computer, closed phone You are allowed two cheat pages Either a standard 8.5x11-sized sheet filled on both sides or two standard
More informationCSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation Hal Perkins Winter 2018 Generics UW CSE 331 Winter 2018 1 Varieties of abstraction Abstraction over computation: procedures (methods) int x1, y1, x2, y2; Math.sqrt(x1*x1
More informationSoftware Engineering. Prof. Agostino Poggi
Agent and Object Technology Lab Dipartimento di Ingegneria dell Informazione Università degli Studi di Parma Software Engineering Generics Prof. Agostino Poggi public class ListOfIntegers { //... fields...
More informationCSCE 314 Programming Languages
CSCE 314 Programming Languages! Java Generics II Dr. Hyunyoung Lee!!! 1 Type System and Variance Within the type system of a programming language, variance refers to how subtyping between complex types
More informationJava 5 New Language Features
Java 5 New Language Features Ing. Jeroen Boydens, BLIN prof dr. ir. Eric Steegmans http://users.khbo.be/peuteman/java5/ Enterprise Programming Last session. Jeroen Boydens 1. JDK v1.4: assert 2. Generics
More informationPIC 20A Collections and Data Structures
PIC 20A Collections and Data Structures Ernest Ryu UCLA Mathematics Last edited: March 14, 2018 Introductory example How do you write a phone book program? Some programmers may yell hash table! and write
More informationExam Review. CSE 331 Section 10 12/6/12. Slides by Kellen Donohue with material from Mike Ernst
Exam Review CSE 331 Section 10 12/6/12 Slides by Kellen Donohue with material from Mike Ernst Course Logistics All homework s done (except late days) HW8 returned HW7 being graded HW9 will be graded during
More informationboolean add(e e); // Makes the collection contain e. // Returns true if e not already there.
1 1. (a) The Java collections classes includes the generic interface Collection for collections with element type E, which the collection classes implement. This includes the following method: boolean
More informationCSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation Kevin Zatloukal Summer 2017 Generics (Based on slides by Mike Ernst, Dan Grossman, David Notkin, Hal Perkins, Zach Tatlock) Preface This lecture will get into the
More informationCSC 172 Data Structures and Algorithms. Lecture 3 Spring 2018 TuTh 3:25 pm 4:40 pm
CSC 172 Data Structures and Algorithms Lecture 3 Spring 2018 TuTh 3:25 pm 4:40 pm Agenda Administrative aspects Java Generics Chapter 1 ADMINISTRATIVE ASPECTS Workshops Workshops Workshops begin on this
More informationCS61B Lecture #24. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.
CS61B Lecture #24 Today: Java support for generic programming Readings for today: A Java Reference, Chapter 10. Readings for Monday: Data Structures, 6.4. Last modified: Fri Oct 19 19:33:03 2012 CS61B:
More informationAnnouncements. Java Graphics. Exceptions. Java Odds & Ends
Java Odds & Ends Lecture 25 CS211 Fall 2005 Final Exam Wednesday, 12/14 9:00-11:30am Uris Aud Review Session Sunday, 12/11 1:00-2:30pm Kimball B11 Check your final exam schedule! Announcements For exam
More informationCSE Lecture 7: Polymorphism and generics 16 September Nate Nystrom UTA
CSE 3302 Lecture 7: Polymorphism and generics 16 September 2010 Nate Nystrom UTA 2 Polymorphism poly = many morph = shape Allow a variable to contain values with different types 3 Subtype polymorphism
More informationCS61B Lecture #23. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.
CS61B Lecture #23 Announcements: Josh s office hours are now back in his office. HW6 now due Saturday. Partial solar eclipse tomorrow, starting at 1:52PM. Next one in August, 2017. See http://www.timeanddate.com/eclipse/list.html
More informationCSE 331 Software Design and Implementation. Lecture 13 Generics 1
CSE 331 Software Design and Implementation Lecture 13 Generics 1 Zach Tatlock / Spring 2018 Varieties of abstraction Abstraction over computation: procedures (methods) int x1, y1, x2, y2; Math.sqrt(x1*x1
More informationAnnouncements. 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 Announcements Leah Perlmutter / Summer 2018 Announcements Quiz 5 is due Thursday Homework 6 due Thursday Midterm grades and feedback will
More informationCSE 331 Software Design and Implementation. Lecture 14 Generics 1
CSE 331 Software Design and Implementation Lecture 14 Generics 1 Leah Perlmutter / Summer 2018 Announcements Announcements Quiz 5 is due Thursday Homework 6 due Thursday Midterm grades and feedback will
More informationCS6202: Advanced Topics in Programming Languages and Systems Lecture 0 : Overview
CS6202: Advanced Topics in Programming Languages and Systems Lecture 0 : Overview Advanced Language Features and Foundations Lecturer : Chin Wei Ngan Email : chinwn@comp.nus.edu.sg Office : S15 06-01 CS6202
More informationComputational Expression
Computational Expression Variables, Primitive Data Types, Expressions Janyl Jumadinova 28-30 January, 2019 Janyl Jumadinova Computational Expression 28-30 January, 2019 1 / 17 Variables Variable is a name
More informationChapter 3 Function Overloading
Chapter 3 Function Overloading Function Overloading. Calling Overloaded Function. Function Overloading When Several Function declaration are specified for a single function name in the same scope, the
More informationSemantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413
CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 13: Types and Type-Checking 19 Feb 07 Semantic Analysis Last time: Semantic errors related to scopes Symbol tables Name resolution This lecture:
More informationLecture #23: Conversion and Type Inference
Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). Last modified: Fri Oct 20 10:46:40 2006 CS164:
More informationCMSC 132: Object-Oriented Programming II. Generic Programming. Department of Computer Science University of Maryland, College Park
CMSC 132: Object-Oriented Programming II Generic Programming Department of Computer Science University of Maryland, College Park Generic Programming Generic programming Defining constructs that can be
More informationConversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";
Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). In Java, this is legal: Object x = "Hello";
More informationObject-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018
Object-oriented programming 1 and data-structures CS/ENGRD 2110 SUMMER 2018 Lecture 1: Types and Control Flow http://courses.cs.cornell.edu/cs2110/2018su Lecture 1 Outline 2 Languages Overview Imperative
More informationINF3110 Programming Languages Object Orientation and Types, part II
INF3110 Programming Languages Object Orientation and Types, part II 10/31/17 1 Object Orientation and Types Lecture I Lecture II - Today From predefined (simple) and user-defined (composite) types via
More informationInheritance. Improving Structure with Inheritance. Dr. Siobhán Drohan Mairead Meagher. Produced by:
Inheritance Improving Structure with Inheritance Produced by: Dr. Siobhán Drohan Mairead Meagher Department of Computing and Mathematics http://www.wit.ie/ Lectures and Labs This weeks lectures and labs
More informationDrStrangebrac<E,T> Or how I learned to stop worrying, and love generics...
DrStrangebrac Or how I learned to stop worrying, and love generics... a presentation to The Seattle Java Users Group by Wilhelm Fizpatrick http://www.agileinformatics.com/ Java is Sun Microsystems,
More informationType Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413
Type Inference Systems CS412/CS413 Introduction to Compilers Tim Teitelbaum Type inference systems define types for all legal programs in a language Type inference systems are to type-checking: As regular
More information1.1. Annotations History Lesson - C/C++
1. Additions Thanks to Dr. James Heliotis. He started it all :) See also here: and see also here: and here: You need to use the tools from the Java release candidate 1 % bash % export PATH=/usr/local/j2sdk1.5.0-rc1/bin:$PATH
More information4. Java language basics: Function. Minhaeng Lee
4. Java language basics: Function Minhaeng Lee Review : loop Program print from 20 to 10 (reverse order) While/for Program print from 1, 3, 5, 7.. 21 (two interval) Make a condition that make true only
More informationCS108, Stanford Handout #8. Java Generics
CS108, Stanford Handout #8 Fall, 2007-08 Nick Parlante Java Generics Java generics (added in version 5) are a mixed bag. Some uses of generics are simple to understand and make the code cleaner. They are
More informationMIT Semantic Analysis. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology
MIT 6.035 Semantic Analysis Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology Error Issue Have assumed no problems in building IR But are many static checks that need
More informationExercise 8 Parametric polymorphism November 17, 2017
Concepts of Object-Oriented Programming AS 2017 Exercise 8 Parametric polymorphism November 17, 2017 Task 1 Implement a list in Java or C# with two methods: public void add(int i, Object el) public Object
More informationGenericity. Philippe Collet. Master 1 IFI Interna3onal h9p://dep3nfo.unice.fr/twiki/bin/view/minfo/sofeng1314. P.
Genericity Philippe Collet Master 1 IFI Interna3onal 2013-2014 h9p://dep3nfo.unice.fr/twiki/bin/view/minfo/sofeng1314 P. Collet 1 Agenda Introduc3on Principles of parameteriza3on Principles of genericity
More informationJava Generics -- an introduction. Based on https://docs.oracle.com/javase/tutorial/java/generics/why.html
Java Generics -- an introduction Based on https://docs.oracle.com/javase/tutorial/java/generics/why.html Generics vs. Templates Templates in C++ are compiled into unique code based on the types passed
More informationModule 11. Collections and Iterators. Adapted from Absolute Java, Rose Williams, Binghamton University
Module 11 Collections and Iterators Adapted from Absolute Java, Rose Williams, Binghamton University Parameterized Classes and Generics Beginning with version 5.0, Java allows class and method definitions
More informationProblems. Java Generics. Example. Example. Often you need the same behavior for different kind of classes
Problems Often you need the same behavior for different kind of classes Java Generics Use Object references to accommodate any object type Use Generic classes and Method The use of Object references induces
More informationGeneric types. Announcements. Raw ArrayLists. Generic types (cont.) Creating a raw ArrayList: Accessing a raw ArrayList:
Announcements PS 3 is ready Midterm exam 1: Tuesday, April 11, in class Closed book but one sheet, both sides, of A4 paper is allowed Today s topic: Generics (parameterized types) Readings for this slide
More informationClosed 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
Announcements PS 3 is ready Midterm exam 1: Tuesday, April 11, in class Closed book but one sheet, both sides, of A4 paper is allowed Today s topic: Generics (parameterized types) Readings for this slide
More informationCOMP6700/2140 Generic Methods
COMP6700/2140 Generic Methods Alexei B Khorev and Josh Milthorpe Research School of Computer Science, ANU March 2017 Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Generic Methods March 2017
More informationCMSC 202. Containers
CMSC 202 Containers Container Definition A container is a data structure whose purpose is to hold objects. Most languages support several ways to hold objects. Arrays are compiler-supported containers.
More informationCollections, Maps and Generics
Collections API Collections, Maps and Generics You've already used ArrayList for exercises from the previous semester, but ArrayList is just one part of much larger Collections API that Java provides.
More informationABSTRACT DATA TYPES: COLLECTIONS, LISTS, SETS, MAP, QUEUES. Thursday, June 30, 2011
1 ABSTRACT DATA TYPES: COLLECTIONS, LISTS, SETS, MAP, QUEUES Lecture 4 CS 2110 Summer 2011 Lists are Iterable 4 for public static void printlist(list strings) { for (int idx = 0; idx < strings.size();
More informationINF3110 Programming Languages Object orientation part II
INF3110 Programming Languages Object orientation part II 11/4/16 1 Object Orientation and Types Lecture I Lecture II - Today From predefined (simple) and user-defined (composite) types via Abstract data
More informationExercise 8 Parametric polymorphism November 18, 2016
Concepts of Object-Oriented Programming AS 2016 Exercise 8 Parametric polymorphism November 18, 2016 Task 1 Consider the following Scala classes: class A class B extends A class P1[+T] class P2[T
More informationPierce Ch. 3, 8, 11, 15. Type Systems
Pierce Ch. 3, 8, 11, 15 Type Systems Goals Define the simple language of expressions A small subset of Lisp, with minor modifications Define the type system of this language Mathematical definition using
More informationData Types. Every program uses data, either explicitly or implicitly to arrive at a result.
Every program uses data, either explicitly or implicitly to arrive at a result. Data in a program is collected into data structures, and is manipulated by algorithms. Algorithms + Data Structures = Programs
More informationJava Collection Framework
Java Collection Framework Readings Purpose To provide a working knowledge of the Java Collections framework and iterators. Learning Objectives Understand the structure of the Java Collections framework
More informationCS558 Programming Languages
CS558 Programming Languages Fall 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Type Inference Some statically typed languages, like ML (and to a lesser extent Scala), offer alternative
More informationBig O & ArrayList Fall 2018 Margaret Reid-Miller
Big O & ArrayList 15-121 Fall 2018 Margaret Reid-Miller Today Exam 1: Thursday, Oct 4, 2018 Exam 2 date: Currently Thur. Oct 25 th Move to Tues Oct 30 or Thur Nov 1? (Withdraw deadline Tues Nov 6.) Homework
More informationAbstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.
a and Interfaces Class Shape Hierarchy Consider the following class hierarchy Shape Circle Square Problem AND Requirements Suppose that in order to exploit polymorphism, we specify that 2-D objects must
More informationCS Programming I: ArrayList
CS 200 - Programming I: ArrayList Marc Renault Department of Computer Sciences University of Wisconsin Madison Spring 2018 TopHat Sec 3 (AM) Join Code: 427811 TopHat Sec 4 (PM) Join Code: 165455 ArrayLists
More informationLecture 7: Type Systems and Symbol Tables. CS 540 George Mason University
Lecture 7: Type Systems and Symbol Tables CS 540 George Mason University Static Analysis Compilers examine code to find semantic problems. Easy: undeclared variables, tag matching Difficult: preventing
More information301AA - Advanced Programming [AP-2017]
301AA - Advanced Programming [AP-2017] Lecturer: Andrea Corradini andrea@di.unipi.it Tutor: Lillo GalleBa galleba@di.unipi.it Department of Computer Science, Pisa Academic Year 2017/18 AP-2018-15: Java
More informationAbstract Data Types (ADTs) Example ADTs. Using an Abstract Data Type. Class #08: Linear Data Structures
Abstract Data Types (ADTs) Class #08: Linear Data Structures Software Design III (CS 340): M. Allen, 08 Feb. 16 An ADT defines a kind of computational entity: A set of objects, with possible values A set
More informationCSE 8B Intro to CS: Java
CSE 8B Intro to CS: Java Winter, 2006 March 7 (Day 17) ArrayList Generics Class that: works like a resizable array ArrayList Has a current capacity: if you add more elements, it ll allocate more space
More informationITI Introduction to Computing II
ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Interface Abstract data types Version of January 26, 2013 Abstract These lecture notes are meant
More informationpublicvoid set(objectobject){ this. object =object;} public Objectget(){ return object;}
1. Consider the following code (Oracle): publicclass Bucket{ private Object object ; publicvoid set(objectobject){ this. object =object; public Objectget(){ return object; Which of the following is an
More information09/02/2013 TYPE CHECKING AND CASTING. Lecture 5 CS2110 Spring 2013
1 TYPE CHECKING AND CASTING Lecture 5 CS2110 Spring 2013 1 Type Checking 2 Java compiler checks to see if your code is legal Today: Explore how this works What is Java doing? Why What will Java do if it
More informationJava Brand Generics. Advanced Topics in Java. Khalid Azim Mughal Version date:
Java Brand Generics Advanced Topics in Java Khalid Azim Mughal khalid@ii.uib.no http://www.ii.uib.no/~khalid/atij/ Version date: 2005-03-03 Khalid Azim Mughal Java Brand Generics 1/40 Overview Introduction
More information1 10 3:30 5: :30 1:30 206, ICICS/CS
Department of Computer Science Undergraduate Events Events this week Resume Editing Drop-In Session Date: Mon., Feb 1 Time: 11 am 2 pm Location: Rm 255, ICICS/CS EADS Info Session Date: Mon., Feb 1 Time:
More informationMore than you ever wanted to know about. Java Generics. Jeff Meister CMSC 420 Summer 2007
More than you ever wanted to know about Java Generics Jeff Meister CMSC 420 Summer 2007 The obligatory review of the boring stuff, or GENERICS: A YOUNG LADY S ILLUSTRATED PRIMER IN FOUR SLIDES Java 1.4:
More informationCS 302: Introduction to Programming
CS 302: Introduction to Programming Lectures 2-3 CS302 Summer 2012 1 Review What is a computer? What is a computer program? Why do we have high-level programming languages? How does a high-level program
More information301AA - Advanced Programming [AP-2017]
301AA - Advanced Programming [AP-2017] Lecturer: Andrea Corradini andrea@di.unipi.it Tutor: Lillo GalleBa galleba@di.unipi.it Department of Computer Science, Pisa Academic Year 2017/18 AP-2017-14: Java
More informationAlgorithms. Produced by. Eamonn de Leastar
Algorithms Produced by Eamonn de Leastar (edeleastar@wit.ie) Collections ± Collections Architecture ± Definition ± Architecture ± Interfaces ± Collection ± List ± Set ± Map ± Iterator ± Implementations
More informationAdvanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II
Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II Mahmoud El-Gayyar elgayyar@ci.suez.edu.eg Ad hoc-polymorphism Outline Method overloading Sub-type Polymorphism Method overriding Dynamic
More informationCompuScholar, Inc. Alignment to Nevada "Computer Science" Course Standards
CompuScholar, Inc. Alignment to Nevada "Computer Science" Course Standards Nevada Course Details: Course Name: Computer Science Primary Cluster: Information and Media Technologies Standards Course Code(s):
More informationThe class Object. Lecture CS1122 Summer 2008
The class Object http://www.javaworld.com/javaworld/jw-01-1999/jw-01-object.html Lecture 10 -- CS1122 Summer 2008 Review Object is at the top of every hierarchy. Every class in Java has an IS-A relationship
More informationTypes. What is a type?
Types What is a type? Type checking Type conversion Aggregates: strings, arrays, structures Enumeration types Subtypes Types, CS314 Fall 01 BGRyder 1 What is a type? A set of values and the valid operations
More informationScience of Computer Programming
Science of Computer Programming 73 (2008) 59 75 Contents lists available at ScienceDirect Science of Computer Programming journal homepage: www.elsevier.com/locate/scico On the reification of Java wildcards
More information