Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
1Activity
0 of .
Results for:
No results containing your search query
P. 1
lecture-03

lecture-03

Ratings: (0)|Views: 6 |Likes:
Published by minhhai2209

More info:

Published by: minhhai2209 on Feb 19, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

05/10/2014

pdf

text

original

15
Lecture \ue001: Object Semantics
\ue000ese notes cover two lectures on the topic of object semantics. \ue000e material is chosen
with three purposes in mind:

\u00b7 to help you become familiar with the basic runtime mechanism common to all ob- ject-oriented languages (but with a particular focus on Java): variables, object refer- ences, assignments, mutability, and so on.

\u00b7 to introduce two diagrammatic notations, object diagrams for describing \u2018snapshots\u2019
(that is, particular con\ufb01gurations of objects in the heap), and\u2014far more useful\u2014ob-
ject models, for describing sets of snapshots.

\u00b7 to make you aware, in passing, of some tricky issues that we\u2019ll return to later in more detail, and which turn out to be of fundamental importance: equality, rep invariants and exposure, and subtype polymorphism.

When you\u2019ve completed this material, you should have a solid grasp of what happens when Java code executes, so that you can predict what some code will do without run- ning it. You should be able to read object models, but it will take some practice before you\u2019re con\ufb01 dent writing them.

\ue001.\ue000 Variables, References and Objects
Some types of objects can be created withliteral s. What happens when you run this?
2.1.1
String a = \u201czeeb\u201d;
2.1.2
System.out.println (a);
It printszeeb. Statement 2.1.1 is a declaration (of the variablea) and an assignment (to
a) in one. \ue000 e expression \u201czeeb\u201d is called a string literal. Statement 2.1.2 is a procedure
call that prints a representation ofa to standard out; don\u2019t worry for now about its de-
tails.
We can draw the result of the \ufb01 rst statement as an object diagram, showing thata is a
reference to an object of typeString:
\ue000 ata is a reference to the string (and not the string itself, or a slot that holds it) is im-
portant to understand, but actually not very signi\ufb01cant in this case.
"zeeb"
(String)
a
16
What happens when you run this?
2.1.3
String a = \u201czeeb\u201d;
2.1.4
String b = a.toUpperCase ();
2.1.5
System.out.println (b);

It printsZEEB. Statement 2.1.4 is a call to the methodtoUpperCase. \ue000e method has one argument,a, which sits in a special position: it\u2019s called the \u2018receiver\u2019. \ue000e call results in the creation of a fresh string that is then bound to the variableb. Here\u2019s the object dia- gram:

What does this do?
2.1.6
String a = \u201czeeb\u201d;
2.1.7
a.toUpperCase ();
2.1.8
System.out.println (a);

It printszeeb. Statement 2.1.7 creates a fresh string that gets thrown away since it is bound to no variable. \ue000e string object referenced bya is not changed: strings are im- mutable.

What about this?
2.1.9
String a = \u201czeeb\u201d;
2.1.10
a = a.toUpperCase ();
2.1.11
System.out.println (a);
Again, no object changes. Buta is made to refer to the new object by the assignment
2.1.10, so the result isZEEB.
"zeeb"
(String)
a
"ZEEB"
(String)
b
"zeeb"
(String)
a
"ZEEB"
(String)
a
17
\ue000.\ue000 Aliasing, Mutability and Reference Equality
Java provides a variety of collections as part of its standard library. A vector is like an
array, but it can grow and shrink dynamically. An example of using a vector:
2.2.1
Vector v = new Vector ();
2.2.2
String a = \u201czeeb\u201d;
2.2.3
v.add (a);
2.2.4
System.out.println (v.lastElement ());
\ue000is printszeeb, because statement 2.2.3 inserts (a reference to) the string (referred to
by)a into the vector (referred to by)v:

and statement 2.2.4 takes this element and prints it. \ue000e methodl a s t E le me nt is like the string methodtoUpperCase: it takes a single argument, the receiver, has no e\ufb00ect on it, and returns a reference to an object (in this case, the last element of the vectorv). \ue000e methodadd, on the other hand, takes two arguments: the \ufb01rst is the receiverv, and the second is the string argumenta. (Sometimes we\u2019ll say that such a method takes only

one argument, ignoring the receiver.) UnliketoUpperCase, the methodadd does not
return an object, but changes ormut ate s its receiver object. Vectors, unlike strings, can
change, and are thus said to bemut able.
To see this, consider:
2.2.5
Vector v = new Vector ();
2.2.6
Vector k = v;
2.2.7
String a = \u201czeeb\u201d;
2.2.8
v.add (a);
2.2.9
k.add (a.toUpperCase());
2.2.10
System.out.println (v.lastElement ());

\ue000is printsZEEB, since after statement 2.2.6 the two variablesk andv are names for the same vector object: they are said to beali a se s. \ue000e calls toadd mutate the one vector object, \ufb01rst adding the object for the lower case string, then the upper case string. \ue000e changes are visible through both names, so in statements 2.2.8 to 2.2.10 we could actu- ally permute the namesk andv without any change in behaviour.

(Vector)
v
"zeeb"
(String)
a
elts[0]

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->