UMBC CMSC 331 - Review of objects and variables in Java

Unformatted text preview:

CMSC 331 Principles of programming languages10/23/2003 1UMBC CMSC 331 JavaReview of objects Review of objects and variables and variables in Javain Java2UMBC CMSC 331 JavaVariables and Objects–what happens when you run this?String a = “foo”;System.out.println (a); –it printsfoo–what is “foo”?–a string literal that evaluates to a String object–what is a?–a variable whose value is an object reference–what is String a = “foo”?–a declaration and an assignment in one“foo”(String)a3UMBC CMSC 331 JavaMethod Calls– what happens when you run this?String a = “foo”;String b = a.toUpperCase ();System.out.println (a); – it printsfoo– what is toUpperCase?a method of class String• type is String -> String• declared as public String toUpperCase ()– what is a.toUpperCase ()?a method call on the object a– does it change a?no, it creates a new string“foo”(String)a“foo”(String)b4UMBC CMSC 331 JavaNull References–what happens when you run this?String a = null;System.out.println (a); –it printsnull–what happens when you run this?String a = null;String b = a.toUpperCase ();System.out.println (b); it throws a NullPointerException–why?because a method call must have a subjectCMSC 331 Principles of programming languages10/23/2003 25UMBC CMSC 331 JavaSharing & Equality– what happens when you run this?String a = “foo”;String b = “foo”;System.out.println (b); – it printsfoo– is that the same as this?String a = “foo”;String b = a;System.out.println (b); – yes, because String is immutable.– There is no way to distinguish these cases and, in fact, Java virtual machine may produce upper or lower state in this case.“foo”(String)a“foo”(String)b“foo”(String)ab6UMBC CMSC 331 JavaMutable Containers–what happens when you run this?Vector v = new Vector ();String a = “foo”;v.addElement (a);System.out.println (v.lastElement ());–it printsfoo–what happens when you run this?Vector v = new Vector ();String a = “foo”;String b = “foo”;v.addElement (a);System.out.println (v.lastElement ());v.addElement (b);System.out.println (v.lastElement ());–it printsfoofoo(Vector)v“foo”(String)a“foo”(String)b7UMBC CMSC 331 JavaAliasing–what about this? Vector v = new Vector ();Vector q = v;String a = “foo”;v.addElement (a);System.out.println (q.lastElement ());–it printsfoo–why?–because v and q are aliased: they arenames for the same object–what if we now do this?if (v == q) System.out.println (“same object”);if (v.equals (q)) System.out.println (“same value”);–it printssame objectsame value(Vector)v“foo”(String)aqAliasing occurs when several different identifiers refer to the same object. The term is very general and is used in many contexts. 8UMBC CMSC 331 JavaAliasing and Immutables–what does this do?String a = “foo”;String b = a;a.toUpperCase ();System.out.println (b); it printsfoo–why?because strings are immutable–The objects created by the toUpperCase method is eventually GCed (garbage collected.)“foo”(String)ab“foo”(String)a“FOO”(String)bCMSC 331 Principles of programming languages10/23/2003 39UMBC CMSC 331 JavaPolymorphism• what does this do?Vector v = new Vector ();Vector e = new Vector ()v.addElement (e);e.addElement (“foo”);System.out.println (((Vector) v.lastElement ()).lastElement ());• it printsfoo• what kind of method is addElement?a polymorphic onetype is Vector, Object -> voiddeclared as public void addElement (Object o)(Vector)v(Vector)e(String)“foo”10UMBC CMSC 331 JavaOn Polymorphism• First identified by Christopher Strachey (1967) and developed by Hindley and Milner, allowing types such as a list of anything. • E.g. in Haskell we can define a function which operates on a list of objects of any type a (a is a type variable). length :: [a] -> Int• Polymorphic typing allows strong type checking as well as generic functions. ML in 1976 was the first language with polymorphic typing. • Ad-hoc polymorphism (aka overloading) is the ability to use the same syntax for objects of different types, e.g. "+" for addition of reals and integers.• In OOP, the term is used to describe variables which may refer at run-time to objects of different classes. 11UMBC CMSC 331 JavaReference Loops• Can one even add v to itself?Vector v = new Vector ();v.addElement(“foo”);v.addElement (v);System.out.println (v.lastElement ())• yes, try it!• and this?v.addElement (5); • no, 5 is a primitive value, not an object(Vector)v“foo”12UMBC CMSC 331 JavaA Pair of Methods• Some types–what are the types of addElement, lastElement?addElement : Vector, Object -> voidlastElement : Vector -> Object• A puzzle–how are x and e related after this?v.addElement (e);x = v.lastElement ();–they denote the same object–can the compiler infer that?–no! not even that x and e have the same classCMSC 331 Principles of programming languages10/23/2003 413UMBC CMSC 331 JavaDowncasts• What does this do?Vector v = new Vector ();String a = “foo”;v.addElement (a);String b = v.lastElement ();System.out.println (b);• Compiler rejects it: v.lastElement doesn’t return a String!• what does this do?Vector v = new Vector ();String a = “foo”;v.addElement (a);String b = (String) v.lastElement ();System.out.println (b);• it printsfoo14UMBC CMSC 331 JavaUpcasting and Downcasting• Suppose we have object O of class C1 with superclass C2• In Java, upcasting is automatic butdowncasting must be explicit.• Upcasting: treating O as a C2• Downcasting: treating O as a C1C2C1O15UMBC CMSC 331 JavaVariable & Object Classes• What does this do?Vector v = new Vector ();String a = “foo”;v.addElement (a);Object o = v.lastElement ();System.out.println (o.getClass ());• It printsjava.lang.String• What’s going on here?– getClass returns an object representing a class– o.getClass () is the class o has at runtime– System.out.println prints a string representation, i.e., the name16UMBC CMSC 331 JavaSome Key Concepts• Variables & objects– variables hold object references (or primitive values like 5)– null is a special object reference• Sharing, equality & mutability– distinct objects can have the same value– state is held in value of instance variables– an object can be mutable (state may change) or immutable– two variables can point to the same object; changing one affects the otherCMSC 331 Principles of programming


View Full Document

UMBC CMSC 331 - Review of objects and variables in Java

Documents in this Course
Semantics

Semantics

14 pages

Java

Java

12 pages

Java

Java

31 pages

V

V

46 pages

Semantics

Semantics

11 pages

Load more
Download Review of objects and variables in Java
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 Review of objects and variables in Java 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 Review of objects and variables in Java 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?