DOC PREVIEW
UMD CMSC 330 - Java and Java Generics

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 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 12 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 12 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 12 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC 330: Organization ofProgramming LanguagesJava and Java GenericsCMSC 330 2Java• Developed in 1995 by Sun Microsystems– Started off as Oak, a language aimed at software forconsumer 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 notdirectly executing on hardware• These days, Java used for a lot of purposes– Server side programming, general platform, etc.CMSC 330 3Java Versions• Java has evolved over the years– Virtual machine quite stable, but source languagehas been getting new features• Will use Java 1.5 (a.k.a Java 5.0) for this class– Installed on Grace machines– We will be using 1.5-specific features, so if you’ve gota different version, you might want to upgrade– Some of the new features in Java 1.5 came as aresponse to pressure from Microsoft’s C#CMSC 330 4Executing Java• Source (.java) compiled into class files (.class)• Class files are verified before they are executed– Verifier repeats type checking of code• Because class files may not have come from Java compiler• Two modes of execution– Interpretation: Byte codes are read in and run byvirtual machine– Just-in-time compilation: Byte code translated on-the-fly into native executable code– Tradeoffs of these approaches?CMSC 330 5Primitives and Objects• Java distinguishes primitives and objects– Primitives are byte, char, short, int, long, float, double– At run time, these are represented directly as thesevalues• Everything else is an object– Represented at run time as a pointer or reference tomemory on the heap– But no pointer arithmetic; no difference between areference to an object and an object itself• Comparison to Ruby? Tradeoffs?CMSC 330 6The Java Library• One the most important features of Java– Provides a huge pile of classes and methods fordoing routine and less routine programming tasks– You’ll find yourself looking through the API for Java afair amount when programming• Java is the first major language to make a largelibrary part of the specification– Other languages are doing the same nowCMSC 330 7Object-Orientation• Java is a class-based, object-oriented language• Classes extend other classes to inherit– The root of the inheritance hierarchy is Object– Why have a root of the hierarchy?• Allows us to assume that all objects have certain methods– Like hashCode(), equals(Object), etc.• Classes also implement interfaces– Interface is like a class with declarations but no code• Classes may extend one other class, but canimplement many interfaces– Multiple inheritance is tricky to understand/implementCMSC 330 8Subtyping• Both inheritance and interfaces allow one classto be used where another is specified– This is really the same idea: subtyping• We say that A is a subtype of B if– A extends B or a subtype of B, or– A implements B or a subtype of BCMSC 330 9Liskov 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, thenS is a subtype of T.– Does our definition of subtyping in terms of extendsand implements obey this principle?CMSC 330 10Polymorphism• 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 canbe applied to many different types• Ad-hoc polymorphism is overloading– Operator overloading in C++– Method overloading in JavaCMSC 330 11A Stack of Integersclass IntegerStack { class Entry { Integer elt; Entry next; Entry(Integer i, Entry n) { elt = i; next = n; } } Entry theStack; void push(Integer i) { theStack = new Entry(i, theStack); } Integer pop() throws EmptyStackException { if (theStack == null) throw new EmptyStackException(); else { Integer i = theStack.elt; theStack = theStack.next; return i; }}}CMSC 330 12Inner Classes• Classes can be nested inside other classes– These are called inner classes• Within a class that contains an inner class, youcan use the inner class just like any other classCMSC 330 13Referring to Outer Class class Stack { ... private int numEntries; class Entry { Integer elt; Entry next; Entry(Integer i) { elt = i; next = null; numEntries++; } } }• Each inner “object” has an implicit reference tothe outer “object” whose method created it– Can refer to fields directly, or use outer class nameCMSC 330 14Other Features of Inner Classes• Outside of the outer class, use outer.innernotation to refer to type of inner class– E.g., Stack.Entry• An inner class marked static does not have areference to outer class– Can’t refer to instance variables of outer class– Must also use outer.inner notation to refer to innerclass• Question: Can Stack.Entry be made static?CMSC 330 15Compiling Inner Classes• The JVM doesn’t know about inner classes– Compiled away, similar to generics– Inner class Foo of outer class A producesA$Foo.class– Anonymous inner class of outer class A producesA$1.class• We’ll see these later• Why are inner classes useful?CMSC 330 16IntegerStack ClientIntegerStack is = new IntegerStack();Integer i;is.push(new Integer(3));is.push(new Integer(4));i = is.pop();• This is OK, but what if we want other kinds ofstacks?– Need to make one XStack for each kind of X– Problems: Code bloat, maintainability nightmareCMSC 330 17Polymorphism 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 18Stack 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


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?