DOC PREVIEW
UMD CMSC 330 - Java and Java Generics

This preview shows page 1-2-3-4-5-6 out of 17 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CMSC 330: Organization of Programming LanguagesJava and Java GenericsCMSC 330 2Java• Developed in 1995 by Sun Microsystems– Started off as Oak, a language aimed at software for consumer electronics– Then the web came along...• Java incorporated into web browsers– Java source code compiled into Java byte code– Executed (interpreted) on Java Virtual Machine• Portability to different platforms• Safety and security much easier, because code is not directly executing on hardware• These days, Java used for a lot of purposes– Server side programming, general platform, etc.2CMSC 330 3Java Versions• Java has evolved over the years– Virtual machine quite stable, but source language has been getting new features• Will use the latest version of Java for this class– If you’ve got an older version, you might want to upgradeCMSC 330 4Subtyping• Both inheritance and interfaces allow one class to be used where another is specified– This is really the same idea: subtyping• We say that A is a subtype of B if–Aextends B or a subtype of B, or–Aimplements B or a subtype of B3CMSC 330 5Liskov Substitution PrincipleIf for each object o1 of type S there is an object o2 oftype T such that for all programs P defined in terms of T,the behavior of P is unchanged when o1 is substituted foro2 then S is a subtype of T.– I.e, if anyone expecting a T can be given an S, then S is a subtype of T.– Does our definition of subtyping in terms of extends and implements obey this principle?CMSC 330 6Polymorphism• Subtyping is a kind of polymorphism– Sometimes called subtype polymorphism– Allows method to accept objects of many types•We saw parametric polymorphism in OCaml– It’s polymorphism because polymorphic functions can be applied to many different types• Ad-hoc polymorphism is overloading– Operator overloading in C++– Method overloading in Java4CMSC 330 7Polymorphism Using Objectclass Stack {class Entry {Object elt; Entry next;Entry(Object i, Entry n) { elt = i; next = n; }}Entry theStack;void push(Object i) {theStack = new Entry(i, theStack);}Object pop() throws EmptyStackException {if (theStack == null)throw new EmptyStackException();else {Object i = theStack.elt;theStack = theStack.next;return i;}}}CMSC 330 8Stack ClientStack is = new Stack();Integer i;is.push(new Integer(3));is.push(new Integer(4));i=(Integer) is.pop();• Now Stacks are reusable– push() works the same– But now pop() returns an Object• Have to downcast back to Integer• Not checked until run-time5CMSC 330 9General Problem• When we move from an X container to an Object container– Methods that take X’s as input parameters are OK• If you’re allowed to pass Object in, you can pass any X in– Methods that return X’s as results require downcasts• You only get Objects out, which you need to cast down to X• This is a general feature of subtypepolymorphismCMSC 330 10Parametric Polymorphism (for Classes)• In Java 1.5 we can parameterize the Stack class by its element type• Syntax:– Class declaration: class A<T> { ... }•Ais the class name, as before•Tis a type variable, can be used in body of class (...)– Client usage declaration: A<Integer> x;•We instantiate A with the Integer type6CMSC 330 11class Stack<ElementType> {class Entry {ElementType elt; Entry next;Entry(ElementType i, Entry n) { elt = i; next = n; }}Entry theStack;void push(ElementType i) {theStack = new Entry(i, theStack);}ElementType pop() throws EmptyStackException {if (theStack == null)throw new EmptyStackException();else {ElementType i = theStack.elt;theStack = theStack.next;return i;}}}Parametric Polymorphism for StackCMSC 330 12Stack<Element> ClientStack<Integer> is = new Stack<Integer>();Integer i;is.push(new Integer(3));is.push(new Integer(4));i = is.pop();• No downcasts• Type-checked at compile time• No need to duplicate Stack code for every usage– line i = is.pop(); can stay the same even if the type of is isn’t an integer in every path through the program7CMSC 330 13Parametric Polymorphism for Methods•Stringis a subtype of Object1. static Object id(Object x) { return x; }2. static Object id(String x) { return x; }3. static String id(Object x) { return x; }4. static String id(String x) { return x; }• Can’t pass an Object to 2 or 4• 3 doesn’t type check• Can pass a String to 1 but you get an Object backCMSC 330 14Parametric Polymorphism, Again•But id() doesn’t care about the type of x– It works for any type• So parameterize the static method:static <T> T id(T x) { return x; }Integer i = id(new Integer(3));– Notice no need to instantiate id; compiler figures out the correct type at usage– The formal parameter has type T, the actual parameter has type Integer8CMSC 330 15Standard Library, and Java 1.5• Part of Java 1.5 (called “generics”)– Comes with replacement for java.util.*• class LinkedList<A> { ...}• class HashMap<A, B> { ... }• interface Collection<A> { ... }– Excellent tutorial listed on references page• But they didn’t change the JVM to add generics– How was that done?CMSC 330 16Translation via Erasure• Replace uses of type variables with Object– class A<T> { ...T x;... } becomes– class A { ...Object x;... }• Add downcasts wherever necessary– Integer x = A<Integer>.get(); becomes– Integer x = (Integer) (A.get());• So why did we bother with generics if they’re just going to be removed?– Because the compiler still did type checking for us– We know that those casts will not fail at run time9CMSC 330 17Limitations of Translation• Some type information not available at run-time– Recall type variables T are rewritten to Object• Disallowed, assuming T is type variable: –new T()would translate to new Object() (error)–new T[n]would translate to new Object[n] (warning)– Some casts/instanceofs that use T• (Only ones the compiler can figure out are allowed)• Also produces some oddities– LinkedList<Integer>.class == LinkedList<String>.class• (These are uses of reflection to get the class object)CMSC 330 18Using with Legacy Code• Translation via type erasure– class A <T> becomes class A• Thus class A is available as a “raw type”– class A<T> { ... }– class B { A x; } // use A as raw type• Sometimes useful with legacy code, but...– Dangerous feature to use, plus unsafe– Relies on implementation of generics, not semantics10CMSC 330 19Subtyping and Arrays• Java has one


View Full Document

UMD CMSC 330 - Java and Java Generics

Documents in this Course
Exam #1

Exam #1

6 pages

Quiz #1

Quiz #1

2 pages

Midterm 2

Midterm 2

12 pages

Exam #2

Exam #2

7 pages

Ocaml

Ocaml

7 pages

Parsing

Parsing

38 pages

Threads

Threads

12 pages

Ruby

Ruby

7 pages

Quiz #3

Quiz #3

2 pages

Threads

Threads

7 pages

Quiz #4

Quiz #4

2 pages

Exam #2

Exam #2

6 pages

Exam #1

Exam #1

6 pages

Threads

Threads

34 pages

Quiz #4

Quiz #4

2 pages

Threads

Threads

26 pages

Exam #2

Exam #2

9 pages

Exam #2

Exam #2

6 pages

Load more
Download Java and Java Generics
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Java and Java Generics and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Java and Java Generics 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?