\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 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.
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:
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.
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
\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.
Now bringing you back...
Please enter your email address below to reset your password. We will send you an email with instructions on how to continue.
Does that email address look wrong? Try again with a different email.